Android版本相機適配問題集合(不斷整理更新中)

SecurityException相關

1、

java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://com.wapchief.jpushim.fileProvider/external_files/temp.jpg from pid=14476, uid=10031 requires the provider be exported, or grantUriPermission()

APi24以下版本的日誌:
com.wapchief.jpushim E/uri=====: file:///storage/emulated/0/temp.jpg
com.wapchief.jpushim E/uritempFile: file:////storage/emulated/0/small.jpg
APi24以上版本的日誌:
com.wapchief.jpushim E/uriBC=====: content://com.wapchief.jpushim.fileProvider/external_files/temp.jpg
com.wapchief.jpushim E/uri=====: content://com.wapchief.jpushim.fileProvider/external_files/temp.jpg

大概意思是需要把提供者導出


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //開啓臨時權限
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            //重點:針對7.0以上的操作
            intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, uri));
            uritempFile = uri;
        } else {
            uritempFile = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/" + "small.jpg");
        }

2、

java.lang.SecurityException: Permission Denial: opening provider com.wapchief.jpushim.fileProvider from ProcessRecord

大意是說權限之類的沒有開放。

解決方案是檢查AndroidManifest下Application標籤中
android:exported屬性是否開啓,
查看代碼中
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);臨時拍照權限是否開啓

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.wapchief.jpushim.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

其他參考:
 FileProvider
Android7.0調用系統相機拍照、訪問相冊問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章