我在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的清单中,我们声明了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);


同样,只有在OS版本5.0.2的Galaxy A5,运行带有Lollipop的Samsung 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) 


如果您有任何想法,请分享,谢谢。

最佳答案

最终,这篇文章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"/>


现在,该应用程序不会崩溃!

关于android - targetSdkVersion低于22的Download Manager的安全异常(exception),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47035050/

10-11 14:45