Android issues reading images from persistent Uri

风格不统一 提交于 2019-12-04 21:11:17

I solved the android.permission.MANAGE_DOCUMENTS issues in the following way, using the very helpful comment section above.

Inside onCreate():

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
else
    intent.setAction(Intent.ACTION_GET_CONTENT);

Inside onActivityResult():

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    final int takeFlags = intent.getFlags()
            & (Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    try {
        getContentResolver().takePersistableUriPermission(uri, takeFlags);
    } catch (Exception e) {
        Log.w(myLog, "Exception: " + e.getMessage());
    }
}

For Android API24, my newer phone, it works for all images that I've tried so far, both those in the Downloads directory, and those from the camera gallery.

HOWEVER:

For Android API17, my older phone, while it does work for images in Downloads, I discover that I get the following exception for some other images on the phone during getContentResolver().openInputStream(uri)

java.lang.SecurityException: Permission Denial: opening provider 
com.android.gallery3d.provider.GalleryProvider from ProcessRecord ... 
requires com.google.android.gallery3d.permission.GALLERY_PROVIDER or 
com.google.android.gallery3d.permission.GALLERY_PROVIDER
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!