Application crashes on Android 10?

别来无恙 提交于 2020-08-05 06:00:26

问题


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

回答1:


Use File provider for Image URI.

try this code:

add code AndroidManifest.xml

<application>
  <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mypackage.myprovider"
    android:exported="false"
    android:grantUriPermissions="true">
   <meta-data
     android:name="android.support.FILE_PROVIDER_PATHS"
     android:resource="@xml/provider_paths"/>
  </provider>
</application>

add provider_paths.xml file in xml folder under res.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
   <external-path name="external_files" path="."/>
</paths>

get uri from bottom code in your activity class.

final Uri data = FileProvider.getUriForFile(context, "myprovider", new File(file_path)); //here your image uri


来源:https://stackoverflow.com/questions/60523050/application-crashes-on-android-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!