我的应用程序在Play Store上运行了两年,运行得很好。但是在过去的两周内,用户突然报告说应用程序在使用时崩溃了。
所有的崩溃都是针对Android 10用户的.
织物日志显示错误为,
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)在这个屏幕上发生的事情是,用户应该捕捉到一个屏幕快照。我保存这个屏幕截图,然后在ImageView上显示它。我通过URI检索屏幕截图.
当我使用URI.的检索图像时,就会发生崩溃
发布于 2021-03-26 07:58:52
让我解释一下你为什么会有这个异常,以及如何处理它。
但是首先,要仔细记住错误消息。
“只有所有者才能与挂起的媒体内容进行交互:// media /外援/图像/媒体/259525”
请记住“所有者”这个词,在我稍后的解释中,你会知道这个词的意义。
我如何遇到这个异常:
我正在开发一个pdf阅读器应用程序,它已经在Play Store上运行。我们也得到了来自crashlytics的同样例外。就像你的情况一样,大多数设备都是Android 10。
为什么这个异常发生在我的案例中,
一些用户过去所做的是,他们开始从网络下载一个pdf文件,然后他们会意识到,pdf的大小太大了,然后他们会暂停下载pdf (也许以后再下载),然后他们会去文件管理器,尝试打开我的应用程序部分下载的pdf文件。此时,当pdf被部分下载时,我的应用程序将崩溃在openInputStream()方法(就像您在openAssetFileDescriptor()上的崩溃一样)
“所有者”一词的意义:
“业主”这个词在这里非常重要。我来告诉你为什么。在我的例子中,最终用户使用下载管理器(在chrome中)下载pdf。因此,从网络中获取文件包、重新组装文件并重新创建原始文件是下载管理器的工作。现在,只要文件没有完全下载并重新组装,下载管理器就会保留该文件的“所有者”。这意味着文件将使用同步进程锁定,只有下载管理器才有释放锁的权限。因此,当文件被完全下载时,chrome将释放该文件的锁,该文件将可供所有其他应用程序访问。
我如何解决我的崩溃:
我用试捕捉块包围了错误行。如果我得到了IllegalStateException,我将向用户敬酒,该文件部分下载或损坏--我做了这样的事情
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();
}为什么安卓10主要是:
这个问题并不仅限于android 10。巧合的是,android推出了大多数服务的更新,比如chrome、google文件等等,这使得其他应用程序可以查看这些部分创建的文件。
你的例子中的:
屏幕截图将被android的屏幕快照管理器捕获(我不记得确切的名称)。因此,只要将捕获的屏幕快照保存到存储中,就无法获得屏幕快照。在较新的android版本中,当捕捉到屏幕快照时,您将得到屏幕快照的快速预览。除非预览进行,否则屏幕快照管理器将拥有该文件。
发布于 2020-03-04 09:50:36
为图像URI使用文件提供程序。
试试下面的代码:
添加代码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>在res.下的xml文件夹中添加provider_paths.xml文件。
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>从活动类的底部代码获取uri .
final Uri data = FileProvider.getUriForFile(context, "myprovider", new File(file_path)); //here your image urihttps://stackoverflow.com/questions/60523050
复制相似问题