我在Lollipop
及以下发生了一次奇怪的崩溃。尝试从服务器下载文件时出现安全异常,但在运行Marshmallow
及以上的设备中,应用程序不会崩溃。的 logcat的:
引起:java.lang.SecurityException:需要WRITE_EXTERNAL_STORAGE权限才能使用DESTINATION_FILE_URI:uid 10229没有android.permission.WRITE_EXTERNAL_STORAGE。
在App grandle
我使用
targetSdkVersion 22
在我们第一次推送Google控制台以定位最新的sdk版本后会发生变化,但现在这将保持为22,因此无需运行时权限。 此外,在App的Manifest中,我们声明了app的许可。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
此外,使用DownloadManager
内的AsynckTask
的代码:
try {
String dirPath = String.format("%5$s/%1$s/Resources/%2$s/%3$s/%4$s/", userID, resource.getiD(), filePackage.getiD(), language, dir.getCanonicalPath());
File makeDirs = new File(dirPath);
makeDirs.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
this.downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(url);
request.setDestinationInExternalFilesDir(getApplicationContext(), null, filePath);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
downloadID = downloadManager.enqueue(request);
同样,仅在运行版本为5.0.2的Galaxy A5,运行带有Lollipop
的三星Galaxy S6的仿真器和运行Kitkat
4.4.4 HTC One的仿真器中观察到此崩溃。
完整stack trace
:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...players.PDFViewer}: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.os.Parcel.readException(Parcel.java:1540)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:475)
at android.content.ContentResolver.insert(ContentResolver.java:1260)
at android.app.DownloadManager.enqueue(DownloadManager.java:1336)
at de.imsystems.crmmobile.players.PDFViewer.onCreate(PDFViewer.java:104)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
如果您有任何想法,请提前分享,分享。
答案 0 :(得分:1)
最后这篇帖子WRITE_EXTERNAL_STORAGE not working on lollipop even though it's set in the manifest帮助了我。我所做的是改变AndroidManifest.xml
文件中的权限,如下所示:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="26" tools:replace="android:maxSdkVersion"/>
现在应用程序没有崩溃!