【问题标题】:Store Image via Android Media Store in new Folder通过 Android Media Store 将图像存储在新文件夹中
【发布时间】:2020-03-22 11:33:02
【问题描述】:

我正在使用以下内容将在我的应用中创建的图像存储在 Android 库中:

    MediaStore.Images.Media.insertImage(contentResolver, bitmap, "SomeTitle", "Description");

这会将图像存储在图片设备文件夹中并将它们添加到图库中。

我现在想为我的应用创建一个特定的图像文件夹,以便图像存储在文件夹“MyApp”而不是“图片”中。我该怎么做?

【问题讨论】:

  • 这取决于您想要的 Android 版本。
  • 那将是版本 8+
  • 使用 .insert() 代替 .insertImage()。并将路径放入内容值中。
  • .insert() 在哪个类?
  • getContentResolver().insert().

标签: android image gallery mediastore


【解决方案1】:

我找到了隐藏在这里的解决方案:https://stackoverflow.com/a/57265702/289782

我在这里引用它,因为原始问题比较老,用户鲍磊的好答案排名较低。

在 API 29 (Android Q) 之前有几种不同的方法可以做到这一点,但所有这些方法都涉及一个或几个 Q 已弃用的 API。在 2019 年,这是一种向后和向前兼容的方法:

(因为是 2019 年所以我会用 Kotlin 写)

/// @param folderName can be your app's name
private fun saveImage(bitmap: Bitmap, context: Context, folderName: String) {
    if (android.os.Build.VERSION.SDK_INT >= 29) {
        val values = contentValues()
        values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + folderName)
        values.put(MediaStore.Images.Media.IS_PENDING, true)
        // RELATIVE_PATH and IS_PENDING are introduced in API 29.

        val uri: Uri? = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        if (uri != null) {
            saveImageToStream(bitmap, context.contentResolver.openOutputStream(uri))
            values.put(MediaStore.Images.Media.IS_PENDING, false)
            context.contentResolver.update(uri, values, null, null)
        }
    } else {
        val directory = File(Environment.getExternalStorageDirectory().toString() + separator + folderName)
        // getExternalStorageDirectory is deprecated in API 29

        if (!directory.exists()) {
            directory.mkdirs()
        }
        val fileName = System.currentTimeMillis().toString() + ".png"
        val file = File(directory, fileName)
        saveImageToStream(bitmap, FileOutputStream(file))
        if (file.absolutePath != null) {
            val values = contentValues()
            values.put(MediaStore.Images.Media.DATA, file.absolutePath)
            // .DATA is deprecated in API 29
            context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        }
    }
}

private fun contentValues() : ContentValues {
    val values = ContentValues()
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png")
    values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    return values
}

private fun saveImageToStream(bitmap: Bitmap, outputStream: OutputStream?) {
    if (outputStream != null) {
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
            outputStream.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

另外,在调用它之前,您需要先拥有 WRITE_EXTERNAL_STORAGE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多