[英]Android: WRITE_EXTERNAL_STORAGE permission not accepted by device
I'm having a rather strange problem with an Android app I'm writing. 我正在编写的Android应用程序存在一个非常奇怪的问题。
In my app, I use an intent to tell the phone's camera app to take a picture for me. 在我的应用程序中,我意图告诉手机的相机应用程序为我拍照。 After I take this picture, I want to register the newly taken photo with the device's MediaStore content provider in order to make it show up in the regular "gallery" app.
拍摄完这张照片后,我想在设备的MediaStore内容提供商中注册新拍摄的照片,以使其显示在常规的“图库”应用中。
To do this, I know I need the WRITE_EXTERNAL_STORAGE permission, but even when it's in my manifest I get a permission denied exception. 为此,我知道我需要WRITE_EXTERNAL_STORAGE权限,但是即使它在清单中,我也会收到一个权限被拒绝的异常。 What do I have to do to get this permission?
我必须怎么做才能获得此许可? I'm running Android 6.0.1.
我正在运行Android 6.0.1。
Here's my permissions XML in the manifest: 这是清单中我的权限XML:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Here's the code that causes the exception: 这是导致异常的代码:
// insert photo into phone's main photo content provider
ContentResolver cr = getActivity().getContentResolver();
try {
MediaStore.Images.Media.insertImage(cr, photoFile.getPath(),
"Image Capture", "Custom Image capture");
} catch (FileNotFoundException foe) {
Log.e(DEBUG, foe.getMessage());
}
And here's the exception I get: 这是我得到的例外:
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2436, uid=10041 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
EDIT : I should probably mention: 编辑 :我可能应该提到:
Right now my app uses getExternalFilesDir(Environment.DIRECTORY_PICTURES)
, which returns the path /sdcard/Android/data/<package>/files/Pictures
to save it's photos. 现在,我的应用程序使用
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
,它返回路径/sdcard/Android/data/<package>/files/Pictures
来保存照片。 I want them to go where the camera app normally puts them, in /sdcard/DCIM/Camera
. 我希望他们去相机应用程序通常放在
/sdcard/DCIM/Camera
。
You should add the android:maxSdkVersion="18"
to the WRITE_EXTERNAL_STORAGE as well. 您还应该将
android:maxSdkVersion="18"
到WRITE_EXTERNAL_STORAGE中。 Your code should look like this: 您的代码应如下所示:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
This may be a problem with respect to you requesting the permission but not being granted the permission by the user. 关于您请求许可但未被用户授予许可的问题。 Have a look at my answer @ https://stackoverflow.com/a/35574084/529691
看看我的答案@ https://stackoverflow.com/a/35574084/529691
And see if that helps. 看看是否有帮助。
You should promt the user to provide you permission: 您应该提示用户向您提供权限:
int externalPermissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (externalPermissionCheck==-1){
askPermissionStorage();
}
private void askPermissionStorage() {
//for media
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//insert explanation
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//if you want to explaian to the user
} else {//if no explanation needed
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
}
The above is for save the picture\\video and this is for the camera permission: 上面是用于保存图片\\视频,这是用于摄像机权限的:
int cameraPermissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if (cameraPermissionCheck == -1) {
askPermissionCamera();
}
private void askPermissionCamera() {
//for camera
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
CAMERA) != PackageManager.PERMISSION_GRANTED) {
//insert explanation
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
//if you want to explaian to the user
} else {//if no explanation needed
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.