[英]How to request WRITE_EXTERNAL_STORAGE permissions in a fragment
I've seen lots of questions about this but I still can't solve my problem. 我已经看到很多关于这方面的问题,但我仍然无法解决我的问题。 I have a fragment called
CameraFragment.java
which opens a camera and saves the taken picture. 我有一个名为
CameraFragment.java
的片段,它打开一个摄像头并保存拍摄的照片。 The problem is, to save that I need to have WRITE_EXTERNAL_STORAGE permissions... I've added these lines to my Manifest.xml: 问题是,为了保存我需要WRITE_EXTERNAL_STORAGE权限...我已经将这些行添加到我的Manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
But in my logcat , when I run this code to save the image: 但是在我的logcat中 ,当我运行此代码来保存图像时:
private void saveImage(Bitmap finalBitmap, String image_name) {
final String appDirectoryName = "Feel";
String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root);
myDir.mkdirs();
String fname = "Image" + image_name + ".jpg";
File file = new File(myDir, fname);
//if (file.exists()) file.delete();
Log.i("LOAD", root + fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I still get this error: W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Imageimagem1.jpg (Permission denied)
我仍然收到此错误:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Imageimagem1.jpg (Permission denied)
I am not sure but I think that this error comes from no permissions to write the file... How can I give this permissions in runtime? 我不确定,但我认为此错误来自无权写入文件...如何在运行时提供此权限?
Click here for run time permissions! 单击此处获取运行时权限! please look into this github.com/Karumi/Dexter
请查看这个github.com/Karumi/Dexter
Here is a class, with a method asking for storage permission 这是一个类,其中一个方法要求存储权限
public class PermissionManager {
public static boolean checkPermissionREAD_EXTERNAL_STORAGE(final Context context) {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(
(Activity) context,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
showDialog("External storage", context,
Manifest.permission.READ_EXTERNAL_STORAGE);
} else {
ActivityCompat
.requestPermissions(
(Activity) context,
new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
101);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
}
Used in your activity: 用于您的活动:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission given",
Toast.LENGTH_SHORT).show();
//saveImage(finalBitmap, image_name); <- or whatever you want to do after permission was given . For instance, open gallery or something
} else {
Toast.makeText(this, "Permission Denied",
Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
In your case, you'll also check for it before saving the file 在您的情况下,您还将在保存文件之前检查它
if (PermissionManager.checkPermissionREAD_EXTERNAL_STORAGE(this)){
saveImage(finalBitmap, image_name);
}
You can use PermissionDispatcher to handle it very gently AFAIK it works in all versions. 您可以使用PermissionDispatcher非常轻柔地处理它AFAIK它适用于所有版本。
Hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.