JiangNan
JiangNan

Reputation: 1

Android 12 crop image get SecurityExectipion

I'm trying to crop media images from MediaStore query,but got this Execption:

 Caused by: java.lang.SecurityException: UID 10160 does not have permission to content://media/external/images/media/48 [user 0]
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:2326)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1758)
        at android.app.Activity.startActivityForResult(Activity.java:5407)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:588)
        at android.app.Activity.startActivityForResult(Activity.java:5365)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)

As I completely understand it's permission related problem, yet I have no idea how to fix it.As far as I can see,the Crop-App has no read permission to [content://media/external/images/media/48],which my-own-app hold.

the code cause this problem as below:

val su = srcUri ?: throw IllegalArgumentException("Source uri is Null")

val f = if (path.isNullOrBlank()) {
    File(
        FileTool.getAppCacheDir(act),
        tempCropName()
    ).also { path = it.absolutePath }
} else {
    File(path!!)
}


val cropUri =
    FileTool.getFileUri(act, f) ?: throw IllegalArgumentException("Failed to get crop uri")
val intent = Intent("com.android.camera.action.CROP").also {
    it.setDataAndType(su, MediaType.IMAGE.value())

    it.putExtra("aspectX", 1)//ratio
    it.putExtra("aspectY", 1)
    it.putExtra("outputX", size)//size
    it.putExtra("outputY", size)
    it.putExtra("scale", true)

    it.putExtra("return-data", false)//no thumbnail got from back intent
    it.putExtra("outputFormat", format)
    it.putExtra(MediaStore.EXTRA_OUTPUT, cropUri)
}

and the SecurityException located at it.setDataAndType(su, MediaType.IMAGE.value()). as for su,the source uri,got like this

 private fun loadImageUriList(
        bucketId: Long,
        context:Context
    ): List<Uri>? {

        val selection = "${MediaStore.Images.Media.BUCKET_ID} = ?"
        val sort = "${MediaStore.Images.Media._ID} DESC"
        val selectionArgs = arrayOf(bucketId.toString())
        val images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

       
        val c = context.contentResolver.query(images, null, selection, selectionArgs, sort)?:return null

        val imageUris = arrayListOf<Uri>()
        try {
            if (c.moveToFirst()) {
                val iid = c.getColumnIndex(MediaStore.MediaColumns._ID)
                do {
                    val imgId = c.getInt(iid)
                    val path = Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imgId.toString()
                    )
                    imageUris.add(path)
                } while (c.moveToNext())
            }
        } finally {
            c.closeQuietly()
        }
        return imageUris
    }
)

I tried

MediaStore.getRedactedUri(resolver,su)

but the problem still ocurred. by the way, crop image from the system camera worked fine.

Now,I have to copy the image from source Uri to my app's directory,then do the crop job(the Intent above).it's so much work to do and ugly,I know I must have missed something,but I look through google developer doc,got nothing.

please ,help.

Upvotes: 0

Views: 225

Answers (0)

Related Questions