我已经对我的清单上的权限访问权限仍然没有保存。我昨天尝试了这个,它保存了我的形象,现在我不能。我不知道这里有什么问题。 logcat说java.io.IOException:open failed:EACCES(Permission denied)
下面是代码
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout content = (RelativeLayout) findViewById(R.id.dashBoard);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File(Environment.getExternalStorageDirectory().getPath()+ imagename+ ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
content.invalidate();
Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
}
});
这里是完整的logcat https://www.dropbox.com/s/dlb6p5i1nd1kxua/Logcat.txt?dl=0
答案 0 :(得分:1)
在需要SDK API 23之后,您需要使用实时权限。
你可以像这段代码那样请求许可。
if (Build.VERSION.SDK_INT >= 23) {
if (checkWritePermission()) {
// your code
} else {
requestStoragePermission();
}
} else {
// your code
}
您需要检查权限并请求使用此方法。
private boolean checkWritePermission() {
int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this, "EXTERNAL STORAGE permission allows us to save image/video. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE_REQUEST_CODE);
}
}
对句柄权限结果使用onRequestPermissionsResult
方法
不要忘记将权限放在清单上
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />