2

I have a SQLite database containing the URI of images (in device memory) of different products in an inventory. I am not able to retrieve an image URI from the database and set it on a ImageView in an activity because of a SecurityException. How do i deal with it ?

I used a button "Add Image" to get the user to choose an image from the gallery. Below is the onActivityResult method.

 @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);
    }
}

The EditorActivity is where the URI is added into the database. And when opened by clicking on a listview item, should display the data of that particular item including the image, but the image is not being displayed.

@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");
    }
}

Here is the log.

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

The line 423 in EditorActivity is the below line.

mImage.setImageURI(Uri.parse(image));

I have put the following permissions as well.

<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE" />

EDIT 1:

If I use DatabaseUtils.dumpCurrentRow(cursor); after the if block in the onLoadFinished method , i got the log as shown below.

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
ShashankAC
  • 1,016
  • 11
  • 25
  • You need to show us the relevant code which is throwing this exception. This looks like you are trying to hit a Google service of some kind. What happens when you paste that URL directly into a web browser? – Tim Biegeleisen Mar 29 '18 at 06:22
  • @Tim Biegeleisen and @ pskink .....i have added the code to make the question clearer. – ShashankAC Mar 29 '18 at 07:06
  • You mean after the if block ? I tried it. I can add the image into database, but when i open again the image doesn't load up into the imageview, i got the same fishy Uri "content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F9836/ORIGINAL/NONE/902604615" – ShashankAC Mar 29 '18 at 07:19
  • Please check the question, i have edited it again. The image Uri is same whether i put the DatabaseUtils.dumpCurrentRow() inside or outside the if block inside the onLoadFinished() method. – ShashankAC Mar 29 '18 at 07:30
  • If I use a log statement inside the onActivityResult method to print out the image URI in the form of a string, i am getting exact same fishy Uri. Does using `toString()` used on a Uri variable change the contents of the Uri ? – ShashankAC Mar 29 '18 at 09:16

1 Answers1

0

If you are debugging on 23 or higher version of android then you have to take the runtime permission for WRITE_EXTERNAL_STORAGE at runtime as Security exception occurs due to permission denied by device.