我知道這是重複的問題,但目前還沒有任何解決方案。我已經使用最新的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
發佈您的堆棧跟蹤。關於運行時間權限呢? –