Android 10 获取相册图片遇到 open failed: EACCES (Permission denied)解决办法

本文转载自:Android 10 open failed: EACCES (Permission denied)

概述

简而言之就是Android 10及以后对权限管理更严苛了,访问别的应用的数据的限制更多。我直接用了第一个方法暂时解决问题了,第二个后面要用再看吧。

Recently for one of the apps that I was working on, we changed the target SDK to API level 29 (Android 10). Ever since the change was made one of our workflows broke. The workflow is simple, we ask the user if they want to upload a logo to their profile. We let the user pick a file from their media gallery and upload this image to the server.

Running this workflow on an Android 10 device failed to upload the images. The error we saw was open failed: EACCES (Permission denied)

So, what changed?
According to the Android documentation.

Apps targeting Android 10 (API level 29) and higher are given scoped access into an external storage device, or scoped storage, by default. Such apps can see only their app-specific directory
Also in the documentation

In order to access any other file that another app has created, including files in a “downloads” directory, your app must use the Storage Access Framework, which allows the user to select a specific file.

Back to our workflow, when a user picks a file from the gallery, there is no guarantee that the file that was picked was added or edited by some other app. So, if the user picks on a file that let’s say belongs to another app we would run into the permission issues. So, how do we solve this?
There are a couple of ways to do this, in this article we will look into two ways.

Opt out of scoped storage

If your app is not ready for the changes that are coming in for Android 10 then you can “opt-out” by setting the flag requestLegacyExternalStorage to false in your manifest.

<manifest ... >
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>

I spoke to one of the developers from the Privacy team during the recent Android dev Summit and their recommendation was to use this solution as a temporary one

Using openFileDescriptor

The idea is to make a copy of the file that the user picks in the media gallery and add it to your application cache directory.
So, the first step is to open the file using openFileDescriptor

val parcelFileDescriptor = context.contentResolver.openFileDescriptor(fileUri, "r", null)

Let’s create an input stream from it.

val inputStream = FileInputStream(parcelFileDescriptor.fileDescriptor)

The next step is to get the file name from the URI, we can write an extension function on ContentResolver class for this. Please note that the logic below is inspired by the sample code from android docs.

fun ContentResolver.getFileName(fileUri: Uri): String {

    var name = ""
    val returnCursor = this.query(fileUri, null, null, null, null)
    if (returnCursor != null) {
        val nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
        returnCursor.moveToFirst()
        name = returnCursor.getString(nameIndex)
        returnCursor.close()
    }

    return name
}

Alternatively, you can always give your own generated file name.
Now that we know how to get the file name, let’s create a file in our application’s cache directory.

val file = File(context.cacheDir, getFileName(context.contentResolver, fileUri))

Now let us make OutputStream out of this file

val outputStream = FileOutputStream(file)

The final step is to copy the contents of the original file to our newly created file in our cache directory.
You can do this step in multiple ways, one easy way is to use the copy function from the IOUtils class(org.apache.commons.io.IOUtils).

IOUtils.copy(inputStream, outputStream)

Piecing it all together, the final code would be something like this.

val parcelFileDescriptor = context.contentResolver.openFileDescriptor(fileUri, "r", null)

parcelFileDescriptor?.let {
    val inputStream = FileInputStream(parcelFileDescriptor.fileDescriptor)
    val file = File(context.cacheDir, context.contentResolver.getFileName(fileUri))
    val outputStream = FileOutputStream(file)
    IOUtils.copy(inputStream, outputStream)
}

Now that your app has a copy of the file that needs to be uploaded, you can now safely upload this to your backend server.

presenter.uploadLogo(file, ....)

This should fix the permission issues you may face.
Let me know in the comments if this did or did not work for you or if you have other solutions I would love to hear them.
Thanks to Pranay for proofreading and giving me valuable technical feedback.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值