我正在尝试将图像保存到相册中具有应用程序名称的相册中,到目前为止,我设法将图像保存到相册中,但是问题是相册名称始终为“ Pictures”,我已经检查了所有其他帖子在那里,对我没有任何帮助...
这是我的代码
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()
我保存图像没有任何问题,我只想更改相册名称。预先谢谢你...
UPDATE
我试图只创建没有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()
而且,我确保清单文件中具有Ask许可权,并且当用户按下按钮时我正在寻求许可...我确保我的应用程序具有读取和写入文件的许可权。 ..
是因为您正在使用Environment.DIRECTORY_PICTURES
您应该创建一个自定义目录,下面是一个示例:
String path = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(path + "/" + "FolderName");
appDirectory.mkdirs();
因此,经过大量的研究,我能够找到解决问题的方法,我要做的就是添加
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/AppName/")
这将在图片文件夹内创建一个您的应用程序名称的文件夹,在图库中,它将与您输入的应用程序名称一起显示...有关更多详细信息,请阅读this link
[我发现不需要编写的大部分代码,最终我删除了所有多余的东西,这是最终结果
fun saveImage(imageView: View, context: Context) {
val fileName: String
val imageToSave = getBitmapFromView(imageView)
val exists:Boolean
ByteArrayOutputStream().apply {
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, this)
fileName = "AppName_" + UUID.nameUUIDFromBytes(this.toByteArray()).toString().replace("-", "") + ".jpg"
}
context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, arrayOf(MediaStore.Images.Media.DISPLAY_NAME), "${MediaStore.Images.Media.DISPLAY_NAME} = '$fileName' ", 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, fileName)
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/AppName/")
}
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()
}
fun getBitmapFromView(view: View): Bitmap {
return Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888).apply {
Canvas(this).apply {
view.draw(this)
}
}
}
[好,让我们解释一下代码,您可以发送任何实际在其中包含图像的图像视图,并且“ getBitmapFromView”将为您获取位图,然后创建文件名,将位图转换为byteArray,然后创建文件名。 ..之后,我们使用“ contentResolver.query”检查文件是否存在,如果存在,则向用户显示已存储该文件的信息,否则将保存图像,希望对其他人有所帮助:)玩的很开心!