Search code examples
androidkotlinmediastoreandroid-10.0scoped-storage

How to copy file from app-specific folder (file:// scheme) to MediaStore Images collection (content:// scheme) in Android Q?


I am trying to copy file from app-specific folder to MediaStore Images collection using this method:

/**
 * Copies file from path of scheme `file://` to Uri of scheme `content://`
 *
 * @param fromPath Example: /storage/emulated/0/com.my.package/FILE/7225832726757260/wang-shaohong-Kh-NfgSYqN0-unsplash.jpg
 * @param toContentUri should be of `content://` scheme. Example: content://media/external_primary/downloads/1515
 */
@Throws(IOException::class)
fun copyFilePathToContentUri(fromPath: String, toContentUri: Uri) {
    AppContext.getAppContext().contentResolver.openOutputStream(toContentUri)?.use { outputStream ->
        FileInputStream(fromPath).use { inputStream ->
            val buffer = ByteArray(1024)
            var length: Int
            length = inputStream.read(buffer)

            while (inputStream.read(buffer).also { length = it } > 0) {
                outputStream.write(buffer, 0, length)
            }
        }
    }
}

The content uri is created with method below:

fun createImagesFile(imagePath: String): Uri? {
    val fileExtension = imagePath.substringAfterLast('.', "")
    if (fileExtension.isBlank()) return null
    val map = MimeTypeMap.getSingleton()
    val mimeType = map.getMimeTypeFromExtension(fileExtension) ?: return null
    if (!mimeType.startsWith("image/")) {
        loge("FileUtils createImagesFile Error. Given file is not of image type")
        return null
    }

    val volumeName = if (hasAndroid10()) MediaStore.VOLUME_EXTERNAL_PRIMARY else MediaStore.VOLUME_EXTERNAL

    val values = ContentValues().apply {
        put(MediaStore.Images.Media.DISPLAY_NAME, "Photo")
        put(MediaStore.Images.Media.MIME_TYPE, mimeType)
        if (hasAndroid10()) {
            put(MediaStore.Images.Media.IS_PENDING, 1)
        }
    }
    val collection = MediaStore.Images.Media.getContentUri(volumeName)

    return Application.getAppContext().contentResolver.insert(collection, values)

}

Resulting image is not openable. What am I doing wrong?


Solution

  • length = inputStream.read(buffer) 
    

    Remove that statement.

    You are reading a lot of bytes but not writing them to the new file.

    So the new file misses the 'header'.

    The new file is shorter. But you did not compare file sizes. –