我知道这是重复的问题,但目前还没有任何解决方案。我已经使用最新的API等级24与测试API 23.我一定要保存在SD卡上的图像与我的应用程序名称文件夹,但文件夹没有创建...在Android的DCIM文件夹下的SD卡上创建文件夹的错误
我有下面的方法写,但它不工作
public void actionScreenShot(View view) {
//enable drawing cache true
imageContainer.setDrawingCacheEnabled(true);
imageContainer.buildDrawingCache(true);
//create image from layout
Bitmap bitmap = Bitmap.createBitmap(imageContainer.getDrawingCache());
//String folder_path = getApplicationContext().getFilesDir().getPath() + "/MyAppTest/"; //It's work properly
//String folder_path = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/MyAppTest/"; //It's work properly
String folder_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/MyAppTest"; //It's not work properly
if (checkFile(folder_path)) {
saveFile(new File(folder_path), bitmap);
} else {
File folder = new File(folder_path);
if (folder.mkdirs()) {
saveFile(folder, bitmap);
} else {
Log.d("Directory", " >> not created");
}
}
}
public void saveFile(File folder, Bitmap bitmap) {
boolean success = false;
try {
File file = new File(folder, "temp.png");
if (file.exists()) file.delete();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
success = checkDatabase(file.getPath());
}catch (Exception e) {
e.printStackTrace();
}
if (success) {
Log.d("FILE", "success");
} else {
Log.d("FILE", "not success");
}
}
public boolean checkFile(String myPath) {
boolean isExist = false;
try {
File file = new File(myPath);
isExist = file.exists();
} catch (Exception e) {
}
return isExist;
}
权限也给出在AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
日志跟踪如下...
10-05 17:08:09.424 2808-2808/packagename D/Directory >> not created
如果提供的解决方案,然后帮我...
感谢....
'但它不工作'。请提供更好的信息。你应该开始告诉你文件夹是否被创建。 – greenapps
如果mkdirs()失败,则不通知用户。在那里举杯祝酒。您应该使用更多日志和吐司声明,以便您可以告诉我们发生了什么,发生了什么以及看到了什么。 – greenapps
发布您的堆栈跟踪。关于运行时间权限呢? –