我无法使用意图发送的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是正确的我已经检查过了。 请帮我解决一下。
答案 0 :(得分:0)
将您的<uses-feature>
和<uses-permission>
元素移至正确的位置:在之外1>元素(通常位于<application>
元素之上)。
您无法将<application>
保存到数据库中。虽然这可能适用于具有Uri
方案的Uri
,但对于file
方案的Uri
则无效。 You only have rights to that content for a brief time, not indefinitely。如果您需要长期访问该图片,则需要将该图片的副本复制到您控制的文件中(例如,在content
中)。
不要在主应用程序线程上加载位图。使用an image-loading library。
一旦getCacheDir()
升至23或更高,您需要处理runtime permissions。
您无法持有targetSdkVersion
权限,因此请将其删除。