我正在尝试将位图保存到文件中,但是android studio抛出异常java.io.FileNotFoundException: /storage/emulated/0/Pictures/savedBitmap.png (Permission denied)
我在做什么错了?
public void save(){
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "savedBitmap.png");
if (file.exists()){
Log.i(TAG, "file is exists");
} else {
Log.i(TAG, "file is not exists");
}
try {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} finally {
if (fos != null) fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
2019-04-30 13:03:03.058 22470-22470/com.example.paint I/PaintView: file is not exists
2019-04-30 13:03:03.059 22470-22470/com.example.paint W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/savedBitmap.png (Permission denied)
答案 0 :(得分:2)
您必须使用WRITE_EXTERNAL_STORAGE权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
运行时权限
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, " Allow the Storage Permission", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_CONSTANT);
return false;
}
答案 1 :(得分:1)
您需要先向您的AndroidManifest.xml添加android.permission.WRITE_EXTERNAL_STORAGE和android.permission.READ_EXTERNAL_STORAGE权限。然后,需要以编程方式检查活动的onCreate()方法中是否启用了这些权限。您可以按照以下步骤进行操作。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<Integer> lPermission = new ArrayList<>();
List<String> stringPermissionList1 = getPermissionList();
for (int i = 0; i < stringPermissionList1.size(); i++) {
lPermission.add(ContextCompat.checkSelfPermission(activity, stringPermissionList1.get(i)));
}
boolean bPermissionDenied = false;
for (int i = 0; i < lPermission.size(); i++) {
int a = lPermission.get(i);
if (PackageManager.PERMISSION_DENIED == a) {
bPermissionDenied = true;
break;
}
}
if (bPermissionDenied) {
String sMessage = "Please allow all permissions shown in upcoming dialog boxes, so that app functions properly";
//make request to the user
List<String> stringPermissionList = getPermissionList();
String[] sPermissions = stringPermissionList.toArray(new String[stringPermissionList.size()]);
//request the permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(sPermissions, PERMISSION_REQUEST_CODE);
}
} else {
doFurtherProcessing();
}
} else {
doFurtherProcessing();
}
}
private List<String> getPermissionList(){
List<String> stringPermissionList=new ArrayList<>();
stringPermissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
stringPermissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return stringPermissionList;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean isAllPermissionGranted = true;
for (int i = 0; i < grantResults.length; i++) {
int iPermission = grantResults[i];
if (iPermission == PackageManager.PERMISSION_DENIED) {
isAllPermissionGranted = false;
break;
}
}
if (isAllPermissionGranted) {
doFurtherProcessing();
} else {
// Prompt the user to grant all permissions
}
}
希望这会有所帮助