android - android.system.ErrnoException: open failed: EACCES (Permission denied)
问题描述
I have tried this link It's explained there must keep Write External Storage permission must outside the application tag. I have already kept it outside.
I am putting my code here :
cvMain.setDrawingCacheEnabled(true);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "DEMO_" + timeStamp;
String path = createWorkingDirectory().getAbsolutePath() + "/" + imageFileName + ".jpg";
File fileTemp = new File(path);
if (!fileTemp.exists()) {
try {
fileTemp.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Bitmap b = cvMain.getDrawingCache();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(fileTemp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
b.compress(Bitmap.CompressFormat.JPEG, 95, fileOutputStream);
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
cvMain.setDrawingCacheEnabled(false);
createWorkingDirectory() method :
private File createWorkingDirectory() {
File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "demo");
if (!directory.exists()) {
directory.mkdirs();
}
return directory;
}
I am getting this error at specific createNewFile() method call time. I have been kept Write permission inside manifest and runtime applied, even though getting this error. What is the issue I could not understand? Please help me in this. Thank you.
解决方案
I got solution regarding above issue.
cvMain.setDrawingCacheEnabled(true);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "DEMO_" + timeStamp;
String path = createWorkingDirectory().getAbsolutePath() + "/" + imageFileName + ".jpg";
File fileTemp = new File(path);
if (!fileTemp.exists()) {
try {
fileTemp.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Bitmap b = cvMain.getDrawingCache();
OutputStream stream = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri filePathUri = FileProvider.getUriForFile(MainActivity.this, "demoApp", fileTemp);
try {
stream = getContentResolver().openOutputStream(filePathUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (stream != null) {
b.compress(Bitmap.CompressFormat.JPEG, 95, stream);
}
} else {
try {
stream = new FileOutputStream(fileTemp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (stream != null)
b.compress(Bitmap.CompressFormat.JPEG, 95, stream);
}
try {
if (stream != null)
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
cvMain.setDrawingCacheEnabled(false);
file_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="demoApp"
path="." />
</paths>
AndroidManifest.xml inside tag
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="demoApp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider" />
</provider>
Added build.gradle(app)
implementation 'com.android.support:support-v4:27.1.1'
推荐阅读
- zeromq - 在客户端使用 ZAP 处理程序的 ZeroMQ 曲线断言
- objective-c - FatSecret API“无效签名”
- html - 正常和初始(css)之间有什么区别
- arrays - 如何将 AD 计算机名称传递给数组?
- html - 如何使用 CSS flexbox 调整背景和前景层的大小
- ruby-on-rails - 如何调用回调?
- javascript - 有条件地将类分配给 PrimeNG p-datatable 行组标题
- r - R download.file 下载没有图像的原始 html
- dotnetbrowser - 如何在 DotNetBrowser 中创建 JSArray?
- python - Tweepy check if a tweet is a retweet