我知道这是重复的问题,但没有任何解决方案。我在API 23上使用了最新的API Level 24和Test 23.我必须使用我的App Name文件夹将图像保存在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
如果有任何解决方案可以帮助我......
...谢谢
答案 0 :(得分:0)
这是在sdcard中创建文件夹的简单代码
// create a File object for the parent directory
File newFolder = new File("/sdcard/newFolder/");
// have the object build the directory structure, if needed.
newFolder.mkdirs();
// create a File object for the output file
File outputFile = new File(newFolder, filename);
// now attach the OutputStream to the file object, instead of a String
FileOutputStream fos = new FileOutputStream(outputFile);
清单文件中的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
问题是您使用的是API 23和24(Marshmallow)。 Google从API 23更改了权限策略。 将目标版本减少到22.它会起作用。如果您想要23或24,则包含在运行时请求权限的代码。