Marshmallow中的存储权限错误

Marshmallow中的存储权限错误

在Lollipop中,下载功能在我的应用程序中运行良好,但是当我升级到Marshmallow时,我的应用程序崩溃并在我尝试从互联网下载到SD卡时出现此错误:


Neither user  nor current process has android.permission.WRITE_EXTERNAL_STORAGE

它抱怨这行代码:


DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

manager.enqueue(request);

我在应用程序外的清单中拥有权限:


<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WAKE_LOCK" />

我清理并重建了项目,但它仍然崩溃。


Helenr
浏览 756回答 4
4回答

翻过高山走不出你

您应该使用以下方法检查用户是否已授予外部存储权限:if&nbsp;(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)&nbsp;==&nbsp;PackageManager.PERMISSION_GRANTED)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Log.v(TAG,"Permission&nbsp;is&nbsp;granted"); &nbsp;&nbsp;&nbsp;&nbsp;//File&nbsp;write&nbsp;logic&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true;}如果没有,您需要让用户授予您的应用权限:ActivityCompat.requestPermissions(this,&nbsp;new&nbsp;String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},&nbsp;REQUEST_CODE);当然这些仅适用于棉花糖设备,因此您需要检查您的应用是否在Marshmallow上运行:&nbsp;if&nbsp;(Build.VERSION.SDK_INT&nbsp;>=&nbsp;23)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//do&nbsp;your&nbsp;check&nbsp;here &nbsp;}还要确保您的活动实施&nbsp;OnRequestPermissionResult整个权限如下所示:public&nbsp;&nbsp;boolean&nbsp;isStoragePermissionGranted()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(Build.VERSION.SDK_INT&nbsp;>=&nbsp;Build.VERSION_CODES.M)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;==&nbsp;PackageManager.PERMISSION_GRANTED)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.v(TAG,"Permission&nbsp;is&nbsp;granted"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.v(TAG,"Permission&nbsp;is&nbsp;revoked"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ActivityCompat.requestPermissions(this,&nbsp;new&nbsp;String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{&nbsp;//permission&nbsp;is&nbsp;automatically&nbsp;granted&nbsp;on&nbsp;sdk<23&nbsp;upon&nbsp;installation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.v(TAG,"Permission&nbsp;is&nbsp;granted"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;}}权限结果回调:@Overridepublic&nbsp;void&nbsp;onRequestPermissionsResult(int&nbsp;requestCode,&nbsp;String[]&nbsp;permissions,&nbsp;int[]&nbsp;grantResults)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;super.onRequestPermissionsResult(requestCode,&nbsp;permissions,&nbsp;grantResults); &nbsp;&nbsp;&nbsp;&nbsp;if(grantResults.length&nbsp;>&nbsp;0&nbsp;&&&nbsp;grantResults[0]&nbsp;==&nbsp;PackageManager.PERMISSION_GRANTED){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.v(TAG,"Permission:&nbsp;"+permissions[0]+&nbsp;"was&nbsp;"+grantResults[0]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//resume&nbsp;tasks&nbsp;needing&nbsp;this&nbsp;permission &nbsp;&nbsp;&nbsp;&nbsp;}}

红颜莎娜

除非有明确要求在外部存储上写入,否则您始终可以选择将文件保存在app目录中。在我的情况下,我不得不保存文件,浪费了2至3天后,我发现我是否更改了存储路径Environment.getExternalStorageDirectory()至getApplicationContext().getFilesDir().getPath()&nbsp;//which&nbsp;returns&nbsp;the&nbsp;internal&nbsp;app&nbsp;files&nbsp;directory&nbsp;path它在所有设备上都像魅力一样。这是因为要在外部存储上写入,您需要额外的权限,但在内部应用程序目录中编写很简单。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android
Java