我有一个SQLite数据库,其中包含清单中不同产品的图像URI(在设备内存中)。由于SecurityException,我无法从数据库中检索图像URI并将其设置在活动中的ImageView上。我该如何处理它?</ p>
我使用了一个按钮&#34;添加图像&#34;让用户从图库中选择图像。下面是onActivityResult方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Uri imageUri = data.getData();
mImageUri = imageUri;
mImage.setImageURI(mImageUri);
mImageString = mImageUri.toString();
values.put(ItemEntry.COLUMN_ITEM_IMAGE, mImageString);
}
}
EditorActivity是将URI添加到数据库中的位置。当通过单击列表视图项打开时,应显示该特定项目的数据,包括图像,但图像未显示。
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
cursor1 = cursor;
// Bail early if the cursor is null or there is less than 1 row in the cursor
if (cursor == null || cursor.getCount() < 1) {
return;
}
// Proceed with moving to the first row of the cursor and reading data from it
// (This should be the only row in the cursor)
if (cursor.moveToFirst()) {
// Find the columns of pet attributes that we're interested in
int nameColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_NAME);
int imageColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_IMAGE);
int quantityColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_QUANTITY);
int costPriceColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_COST_PRICE);
int sellingPriceColumnIndex = cursor.getColumnIndex(ItemEntry.COLUMN_ITEM_SELLING_PRICE);
// Extract out the value from the Cursor for the given column index
String name = cursor.getString(nameColumnIndex);
String image = cursor.getString(imageColumnIndex);
int quantity = cursor.getInt(quantityColumnIndex);
int costPrice = cursor.getInt(costPriceColumnIndex);
int sellingPrice = cursor.getInt(sellingPriceColumnIndex);
final Cursor cursor1 = cursor;
// Update the views on the screen with the values from the database
mNameEditText.setText(name);
mImage.setImageURI(Uri.parse(image));
mQuantityEditText.setText(Integer.toString(quantity));
mCostPriceEditText.setText(Integer.toString(costPrice));
mSellingPriceEditText.setText(Integer.toString(sellingPrice));
Log.v("OnLoadFinished ", "finished loading data");
}
}
这是日志。
03-29 11:46:38.417 29847-29847/com.example.android.items W/ImageView: Unable to open content: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F9836/ORIGINAL/NONE/269037988
java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.contentprovider.impl.MediaContentProvider from ProcessRecord{2afc008 29847:com.example.android.items/u0a172} (pid=29847, uid=10172) that is not exported from uid 10102
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3628)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4815)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1466)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1087)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
at android.widget.ImageView.resolveUri(ImageView.java:847)
at android.widget.ImageView.setImageURI(ImageView.java:464)
at android.support.v7.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:124)
at com.example.android.items.EditorActivity.onLoadFinished(EditorActivity.java:423)
at com.example.android.items.EditorActivity.onLoadFinished(EditorActivity.java:32)
at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:483)
at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:451)
at android.content.Loader.deliverResult(Loader.java:144)
at android.content.CursorLoader.deliverResult(CursorLoader.java:109)
at android.content.CursorLoader.deliverResult(CursorLoader.java:97)
at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:265)
at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:92)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-29 11:46:38.417 29847-29847/com.example.android.items I/System.out: resolveUri failed on bad bitmap uri: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F9836/ORIGINAL/NONE/269037988
03-29 11:46:38.420 29847-29847/com.example.android.items V/OnLoadFinished: finished loading data
03-29 11:46:38.526 29847-29871/com.example.android.items D/OpenGLRenderer: endAllActiveAnimators on 0xb8bb6578 (ListView) with handle 0xb8dbd038
EditorActivity中的第423行是以下行。
mImage.setImageURI(Uri.parse(图像));
我也提供了以下权限。
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
编辑1:
如果我在onLoadFinished方法的if块之后使用DatabaseUtils.dumpCurrentRow(cursor);
,我得到了如下所示的日志。
03-29 12:46:40.085 7846-7846/com.example.android.items I/System.out: resolveUri failed on bad bitmap uri: content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F9836/ORIGINAL/NONE/902604615
03-29 12:46:40.087 7846-7846/com.example.android.items V/OnLoadFinished: finished loading data
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: 0 {
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: _id=1
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: name=CV Box
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: image=content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F9836/ORIGINAL/NONE/902604615
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: quantity=25
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: costPrice=25
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: sellingPrice=26
03-29 12:46:40.087 7846-7846/com.example.android.items I/System.out: }
03-29 12:46:40.213 7846-7887/com.example.android.items D/OpenGLRenderer: endAllActiveAnimators on 0xb8ec89c8 (RippleDrawable) with handle 0xb8c86f08
答案 0 :(得分:0)
如果您在23或更高版本的android上进行调试,那么您必须在运行时获取WRITE_EXTERNAL_STORAGE的运行时权限,因为设备拒绝了安全性异常。