简体   繁体   English

Android 更改存储图像的图库相册名称

[英]Android Change the Gallery Album name of stored Images

I am trying to save images to an album with the app name inside the gallery, so far I managed to save the image into the gallery but the problem is that the album name is always "Pictures", I've checked all of the other posts out there and nothing worked for me...我正在尝试将图像保存到图库中带有应用程序名称的相册中,到目前为止我设法将图像保存到图库中,但问题是相册名称始终为“图片”,我已经检查了所有其他在那里发帖,对我没有任何作用......

here is my code这是我的代码

val fileName = "abc"
val ImageToSave /*the image that I save, I send it value through method*/

val imageDir = File(activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES),"appName")
val image = File(imageDir,fileName)

if (!imageDir.exists())
    imageDir.mkdirs()

val contentValues = ContentValues()
contentValues.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
contentValues.put(MediaStore.Images.Media.DATA, image.absolutePath)
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
contentValues.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME, "appName")

val url = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)!!
val out = contentResolver.openOutputStream(url)

imageFromView.compress(Bitmap.CompressFormat.JPEG, 100, out)

Toast.makeText(activity, "saved", Toast.LENGTH_SHORT).show()

I am saving the image without any problem I just want to change the album name.我正在保存图像没有任何问题我只想更改相册名称。 thank you in advance...先感谢您...

UPDATE更新

I tried to just create the image file without the contentValue but it appears that something is wrong with the file I keep getting an error that says "File does not exist" here is my code now我试图只创建没有 contentValue 的图像文件,但似乎文件有问题我不断收到一个错误,提示“文件不存在”这是我的代码

val imageDir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "appName")

if (!imageDir.exists())
    imageDir.mkdirs()

val image = File(imageDir, fileName)

if (!image.exists()) {

    val out = FileOutputStream(image)
    ImageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out)

    Toast.makeText(activity, "saved", Toast.LENGTH_SHORT).show()
} else
    Toast.makeText(activity, "Already saved", Toast.LENGTH_SHORT).show()

also, I made sure that I have the ask permission in my manifest file, and I am asking for permission when the user presses on the button... I made sure that my application has permission to read and write to files....此外,我确保我的清单文件中具有询问权限,并且当用户按下按钮时我正在请求权限......我确保我的应用程序具有读取和写入文件的权限......

After a lot of research, I was able to find a solution for my problem, all I had to do is add经过大量研究,我能够为我的问题找到解决方案,我所要做的就是添加

put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/AppName/")

this will create a folder with your app name inside the pictures folder, in the gallery, it will appear with the app name you entered... for extra detail read this link这将在图片文件夹中创建一个包含您的应用程序名称的文件夹,在图库中,它将显示您输入的应用程序名称...有关更多详细信息,请阅读此链接

I found out that a huge chunk of the code that I wrote was not needed,I ended up removing all the extra stuff, this is the end result我发现不需要我写的大量代码,我最终删除了所有额外的东西,这就是最终结果

UPDATE更新

I've updated the code so it works with the old versions of android as well我已经更新了代码,因此它也适用于旧版本的 android

fun saveImage(itemImage: View, context: Context) {
    val imageName: String
    val imageToSave = getBitmapFromView(itemImage)
    val exists: Boolean

    ByteArrayOutputStream().apply {
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, this)
        imageName = "ChatOut_" + UUID.nameUUIDFromBytes(this.toByteArray()).toString().replace("-", "") + ".jpg"
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
        context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,arrayOf(MediaStore.Images.Media.DISPLAY_NAME), "${MediaStore.Images.Media.DISPLAY_NAME} = '$imageName' ", null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC").let {
            exists = it?.count ?: 0 >= 1
            it?.close()
        }


        if (!exists) {
            val contentValues = ContentValues().apply {
                put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
                put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
                put(MediaStore.Images.Media.DISPLAY_NAME, imageName)
                put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/ChatOut/")
            }

            val url = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)!!
            val out = context.contentResolver.openOutputStream(url)
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out)

             Toast.makeText(context, "saved", Toast.LENGTH_SHORT).show()
        } else
            Toast.makeText(context, "Already saved", Toast.LENGTH_SHORT).show()
    }else{
        val imageDir = File("${Environment.getExternalStorageDirectory()}/ChatOut/")
        if (!imageDir.exists())
            imageDir.mkdirs()

        val image = File(imageDir,imageName)

        if (!image.exists()){
            val outputStream = FileOutputStream(image)
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
            outputStream.close()

            val contentValues = ContentValues().apply {
                put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
                put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
                put(MediaStore.Images.Media.DISPLAY_NAME, imageName)
                put(MediaStore.Images.Media.DATA, image.absolutePath)
            }

            context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

            Toast.makeText(context, "saved", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(context, "Already saved", Toast.LENGTH_SHORT).show()
        }
    }
}

fun getBitmapFromView(view: View): Bitmap {
    return Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888).apply {
        Canvas(this).apply {
            view.draw(this)
        }
     }
}

I hope this helps others :) have a good time!我希望这可以帮助其他人:) 玩得开心!

It is because you are using Environment.DIRECTORY_PICTURES这是因为您使用的是Environment.DIRECTORY_PICTURES

You should create a custom directory instead, here is an example:您应该创建一个自定义目录,这是一个示例:

String path = Environment.getExternalStorageDirectory().toString();
                File appDirectory = new File(path + "/" + "FolderName");  
                appDirectory.mkdirs();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM