我在这里设置了意图标志:
public void createAlbum(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
String intentChooserDialog = getResources().getString(R.string.pick_image_dialog);
startActivityForResult(Intent.createChooser(intent, intentChooserDialog), PICK_IMAGE);
}
在我的活动结果中,我设置了可持久权限,因为它们被解释为here:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE) {
// Get image selection
if (data != null) {
ClipData clipData = data.getClipData();
if (clipData != null) {
int clipDataCount = clipData.getItemCount();
if (clipDataCount > 1) {
int clipIndex = 0;
BitmapUri[] bitmapUris = new BitmapUri[clipDataCount];
while (clipIndex < clipDataCount) {
ClipData.Item item = clipData.getItemAt(clipIndex);
Uri uri = item.getUri();
int takeFlags = data.getFlags();
takeFlags &= Intent.FLAG_GRANT_READ_URI_PERMISSION;
getContentResolver().takePersistableUriPermission(uri, takeFlags);
BitmapUri bitmapUri = new BitmapUri(Uri.decode(uri.toString()), data.getFlags());
bitmapUris[clipIndex] = bitmapUri;
clipIndex++;
}
DatabaseAlbumWriter dab = new DatabaseAlbumWriter(this);
dab.execute(bitmapUris);
}
} else {
int oneImageSelected = 1;
BitmapUri[] bitmapUris = new BitmapUri[oneImageSelected];
Uri uri = data.getData();
int takeFlags = data.getFlags();
takeFlags &= Intent.FLAG_GRANT_READ_URI_PERMISSION;
getContentResolver().takePersistableUriPermission(uri, takeFlags);
BitmapUri bitmapUri = new BitmapUri(Uri.decode(uri.toString()), data.getFlags());
bitmapUris[0] = bitmapUri;
DatabaseAlbumWriter dab = new DatabaseAlbumWriter(this);
dab.execute(bitmapUris[0]);
}
}
}
}
在将内容样式Uri保存到sqlite3数据库之后,我想在gridview中加载它们,但是如描述here所述异步。这是我从培训指南中实现的decodeSampledBitmapFromResource。传入具有解码的Uri字符串和来自原始意图的可持久权限的对象。这些值保存在数据库中并再次加载到此处。
protected Bitmap decodeSampledBitmapFromResource(BitmapUri bitmapUri, int reqWidth, int reqHeight) {
Bitmap bitmap = null;
Uri uri = Uri.parse(bitmapUri.getStringUri());
int permissions = bitmapUri.getPermissions();
permissions &= Intent.FLAG_GRANT_READ_URI_PERMISSION;
try {
getContentResolver().takePersistableUriPermission(uri, permissions);
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Rect rect = new Rect(-1,-1,-1,-1);
BitmapFactory.decodeFileDescriptor(fileDescriptor, rect, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, rect, options);
parcelFileDescriptor.close();
} catch (NullPointerException | SecurityException | IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
我在这里得到了这个错误:
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: java.lang.SecurityException: No persistable permission grants found for UID 10058 and Uri 0 @ content://com.android.providers.media.documents/document/image:32
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at android.os.Parcel.readException(Parcel.java:1599)
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at android.os.Parcel.readException(Parcel.java:1552)
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at android.app.ActivityManagerProxy.takePersistableUriPermission(ActivityManagerNative.java:4217)
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at android.content.ContentResolver.takePersistableUriPermission(ContentResolver.java:1703)
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at com.example.app.AlbumCreator$BitmapWorkerTask.decodeSampledBitmapFromResource(AlbumCreator.java:395)
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at com.example.app.AlbumCreator$BitmapWorkerTask.doInBackground(AlbumCreator.java:343)
05-08 14:17:40.850 2728-2813/com.example.app W/System.err: at com.example.app.AlbumCreator$BitmapWorkerTask.doInBackground(AlbumCreator.java:329)
如果我在活动结果中使用BitmapFactory.decodeFileDescriptor从parcelFileDescriptor加载位图,我不会收到此错误。因此,根据我的判断,AsyncTask是具有不同权限的不同用户。任何人都知道如何为AsyncTask分配可持久权限?我宁愿不在UI线程上加载位图。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".AlbumCreator"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".AlbumEditor"
android:label="@string/title_activity_album_editor"
android:parentActivityName=".AlbumCreator">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.AlbumCreator" />
</activity>
</application>
</manifest>
答案 0 :(得分:0)
我解决了自己的问题。如果你查看我的代码,你会发现我使用了intent.getData()。然后我坚持与那个uri的许可。当我保存uri供以后使用时,我解码了它。 Uri.decode(uri.toString()
。
编码的Uri:content://com.android.providers.media.documents/document/image%3A32
解码:content://com.android.providers.media.documents/document/image:32
所以,我改为使用了intent.getDataString(),然后使用Uri.parse()为takePersistableUriPermission()方法创建一个Uri。然后我保存了数据库中intent的初始数据字符串,它保留了编码组件。
答案 1 :(得分:-1)
您需要在Manifest中添加必要的权限。 请添加:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果您想进行写操作,这是必要的。
尝试一下,让我们知道这是否有帮助。