我有一个正在商店中运行的项目,突然间客户开始抱怨无法使用我的应用程序捕获图像,尽管我在这部分没有做任何更改,但我只是更新了android并进行了新的发行与捕获图像无关
这是我的代码
@Override
public void saveImage(PictureTransaction xact, Bitmap bitmap) {
String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, getPhotoFilename(), null);
if(path == null) {
final Activity activity = getActivity();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, getString(R.string.photo_save_error_toast), Toast.LENGTH_SHORT).show();
mTakePictureBtn.setEnabled(true);
mProgressDialog.dismiss();
}
});
} else {
Uri contentUri = Uri.parse(path);
final Image image = getImageFromContentUri(contentUri);
// run the media scanner service
// MediaScannerConnection.scanFile(getActivity(), new String[]{path}, new String[]{"image/jpeg"}, null);
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri));
// the current method is an async. call.
// so make changes to the UI on the main thread.
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
((ImagePickerActivity) getActivity()).addImage(image);
mTakePictureBtn.setEnabled(true);
mProgressDialog.dismiss();
}
});
}
}
后台发生的错误如下
Failed to insert image
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=5494, uid=10570 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1693)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1280)
at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:1025)
at nl.changer.polypicker.CwacCameraFragment$DemoCameraHost.saveImage(CwacCameraFragment.java:172)
at nl.changer.polypicker.camera.ImageCleanupTask.run(ImageCleanupTask.java:136)
当然,我已在清单中的应用程序中以及在运行时权限中请求了 READ_EXTERNAL_STORAGE , WRITE_EXTERNAL_STORAGE , CAMERA ,并且用户接受了对话框,但仍给出该错误
任何人都可以帮忙吗?
编辑
if (PermissionManager.getInstance().hasSelfPermissions(getActivity(), PermissionManager.getInstance().getCameraAndStoragePermissions()) == false) {
mImageAndVideoParentLayout.setVisibility(View.GONE);
PermissionManager.getInstance().checkForCameraAndStoragePermissions2((Fragment) this, PermissionManager.CAMERA_STORAGE_PERMISSIONS);
} else {
mImageAndVideoParentLayout.setVisibility(View.VISIBLE);
}
------------------------
public boolean hasSelfPermissions(Context context, String... permissions) {
for (String permission : permissions) {
if (!hasSelfPermission(context, permission)) {
return false;
}
}
return true;
}
-----------------------
public String[] getCameraAndStoragePermissions() {
return mCameraAndStoragePermissions;
}
-------------------------------
private String [] mCameraAndStoragePermissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
---------------------------
public void checkForCameraAndStoragePermissions2(Fragment fragment, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fragment.requestPermissions(mCameraAndStoragePermissions, requestCode);
} else {
Log.i("AMIRA", "Below M, permissions not via code");
}
}
--------------------------
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.i("AMIRA", "PostAdFragment : onRequestPermissionsResult " + requestCode);
if (requestCode == PermissionManager.LOCATION_PERMISSIONS) {
initalizeMap();
} else if (requestCode == PermissionManager.CAMERA_STORAGE_PERMISSIONS) {
mImageAndVideoParentLayout.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:1)
您是否处理了用户拒绝权限或单击“不再显示”时发生的情况?您应该在禁用权限时禁用该功能,或者一次又一次提示直到获得权限为止。
编辑:
Android现在使用maxSdkVersion过滤器来限制WRITE_EXTERNAL_STORAGE权限。在某些情况下,例如如果应用程序包含需要此权限的依赖项,则maxSdkVersion使得这些依赖项无法授予或请求该权限。这些情况的解决方案是稍后在依赖关系树中声明tools:node =“ replace”清单合并策略:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>