簡體   English   中英

無法加載意圖從圖庫中選擇圖像的Uri發送的位圖照片

[英]Can't load bitmap photo from Uri sent by intent to choose image from gallery

我無法使用意圖從圖庫中選擇圖像發送的uri加載位圖照片。 錯誤信息:

03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/78476 from pid=3693, uid=10246 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.os.Parcel.readException(Parcel.java:1629)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1117)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:955)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:675)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:876)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at lcukerd.com.stufflist.showList.onResume(showList.java:235)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1326)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.Activity.performResume(Activity.java:6338)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3336)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3384)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.ActivityThread.access$900(ActivityThread.java:150)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.os.Looper.loop(Looper.java:168)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5885)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-18 21:16:12.810 3693-3693/lcukerd.com.stufflist W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

意圖創建:

Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent,SELECT_FILE);

與該部分有關的ActivityResult代碼段:

 else if (requestCode==SELECT_FILE)
        {
            if (resultCode== Activity.RESULT_OK)
            {

                photoURI = data.getData();
                Log.d("Gallery call","opened with result " + photoURI.toString());

            }
            else {
                Log.e("Gallery call", "Failed");
                photoURI = null;
            }
        }
    }

初始化發生錯誤的位圖:

if(photoURI!=null) {
                try {

                    Bitmap photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(photoURI));
                    Log.d("Size of image", "width:"+photo.getWidth()+" height:"+photo.getHeight());

                    BitmapDrawable ob = new BitmapDrawable(getResources(), photo);        //Don't ask why , it is required for my code
                    Eimage.setBackground(ob);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("Couldn't Load",name+" "+photoURI);
                }
            }

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="lcukerd.com.stufflist">

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".StartActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".addItem" />

        <uses-feature
            android:name="android.hardware.camera"
            android:required="false" />

        <uses-permission
            android:name="android.permission.WRITE_EXTERNAL_STORAGE"
            android:maxSdkVersion="18" />
        <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="lcukerd.com.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

        <activity android:name=".showList"
            android:windowSoftInputMode="stateHidden"></activity>
        <activity android:name=".orderevent"></activity>
        <activity android:name=".orderitem"></activity>

    </application>

</manifest>

我將uri作為String存儲在SQL中,然后在初始化位圖之前讀取它。 另外,我已經檢查了意圖發送的Uri是正確的。 請幫我解決。

  1. 移動您的<uses-feature><uses-permission>元件到其正確的位置: <application>元件(典型地高於<application>元素)。

  2. 您無法將Uri保存到數據庫。 雖然這可能適用於具有file方案的Uri ,但不適用於具有content方案的Uri 您僅短暫地擁有對該內容的權利,而不是無限期地 如果需要長期訪問該映像,則需要將該映像的副本復制到您控制的文件中(例如,在getCacheDir() )。

  3. 不要在主應用程序線程上加載位圖。 使用圖像加載庫

  4. 一旦targetSdkVersion上升到23或更高,就需要處理運行時權限

  5. 您無法持有MANAGE_DOCUMENTS權限,因此只需刪除該權限即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM