Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application crashes on Android 10?

My application was on Play Store for 2 years and it was running all fine. But suddenly within the past two weeks, users have reported application crashing while using it.

All the crashes are for Android 10 users.

Fabric logs display error as,

java.lang.IllegalStateException: Only owner is able to interact with pending media content://media/external/images/media/259525
        at android.os.Parcel.createException(Parcel.java:2079)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:151)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:705)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1687)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1503)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1420)
        at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:277)
        at android.graphics.ImageDecoder.decodeDrawableImpl(ImageDecoder.java:1743)
        at android.graphics.ImageDecoder.decodeDrawable(ImageDecoder.java:1736)
        at android.widget.ImageView.getDrawableFromUri(ImageView.java:1022)
        at android.widget.ImageView.resolveUri(ImageView.java:991)
        at android.widget.ImageView.setImageURI(ImageView.java:568)
        at androidx.appcompat.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:116)
        at androidx.databinding.adapters.ImageViewBindingAdapter.setImageUri(ImageViewBindingAdapter.java:46)

What's happening at this screen is, The user is supposed to capture a screen-shot. I save this screen-shot and display it later on an ImageView. I retrieve the screen-shot image by it's URI.

The crash is happening when I am retrieving the image using it's URI.

  1. App Compile SDK version: 28
  2. App Target SDK version: 28
like image 937
iCantC Avatar asked Mar 04 '20 09:03

iCantC


People also ask

Why are my apps closing automatically Android 11?

Clear the cache Try clearing out the affected app's cache and data to resolve the issue. Open your phone's settings and tap on Apps & notifications. Tap on Show all apps. Find and tap on the app that's crashing.


1 Answers

Let me explain you what is the exact reason why you are getting this exception and how to tackle it

But first, carefully remember your error message

"Only owner is able to interact with pending media content://media/external/images/media/259525"

Please remember the word "owner" as later in my explanation you will know the significance of this word.

How I encountered this exception:

I am working on a pdf reader application that is already live on Play Store. We also got this same exception from crashlytics. Just like your's case most of the devices were Android 10.

Why this exception happened in my case

What some users used to do is, they would start downloading a pdf file from network, then they would realize that, the pdf size is too big, then they would pause the pdf download(maybe for downloading later), then they would go to file manager and try to open that partially downloaded pdf file with my application. At this point of time as the pdf is partially downloaded, my application would crash at openInputStream() method (Just like yours crash at openAssetFileDescriptor())

Significance of word "Owner":

The word "owner" is very important here. Let me tell you why. In my case, the end user used to download pdf using download manager(inside chrome). So it is the hands of download manager to fetch the packets of file from network, re-assemble them and recreate the original file. Now, as long as the file is not fully downloaded and re-assembled, download manager will stay the "owner" of that file. That means the file will be locked with a synchronized process for which only download manager has the right the release lock. So, when a file is fully downloaded, the chrome will release the lock for that file and that file will be accessible for all other applications.

How I solved my crash:

I surrounded the error line with try-catch block. If I get IllegalStateException I'll show a toast to user that, the file is partially downloaded or corrupted I did something like this

    try{
        try (InputStream in = getContentResolver().openInputStream(uri);)
        {
                 -------
                 -------
        } catch (IOException | SecurityException e) {
                Timber.e(e, "Could not save work file");
                finishError(getString(R.string.mopria_document_reading_error));
        }
    }catch(IllegalStateException exception){
            Toast.makeText(getApplicationContext(), "The file is either partially 
                     downloaded or corrupted", Toast.LENGTH_LONG).show();
            finishAndRemoveTask();
        }

Why Android 10 mostly:

The issue is not specific to android 10 only. The coincidence is android rolled out updates for majority services like chrome, google files and many more that, allows other apps to view these partially created files.

In your case:

The screenshot will be captured by android's screen shot manager(I don't remember the exact name). So, as long as the captured screen shot is saved to storage you won't be able to get the screenshot. In newer android versions, when a screen shot is captured, you will get a quick preview of that screen shot. Unless that preview goes, screenshot manager will be owner of that file.

like image 108
Shriharsh Pathak Avatar answered Oct 30 '22 11:10

Shriharsh Pathak