我想寫一個圖像文件到公共圖庫文件夾中的特定目錄,但我不斷收到一個錯誤,我無法打開該文件,因爲它的一個目錄。我如何保存在外部存儲庫中的圖像在android
我至今是以下
//set the file path
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
File outputFile = new File(path,"testing.png");
outputFile.mkdirs();
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
其中directory是應用程序的名稱。因此,所有應用程序中保存的照片將進入該文件夾/目錄,但我不斷收到錯誤
/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)
即使我不要試圖把它放在一個目錄,並投下變量路徑作爲文件像
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
我沒有得到錯誤,但照片仍未顯示在畫廊中。
***回答 問題是,當我最初運行這個代碼時,它創建了一個名爲testing.png的DIRECTORY,因爲我在創建目錄中的文件之前未能創建目錄。因此,解決辦法是使目錄第一,然後寫進去有一個單獨的文件,像這樣
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + directory;
//directory is a static string variable defined in the class
//make a file with the directory
File outputDir = new File(path);
//create dir if not there
if (!outputDir.exists()) {
outputDir.mkdir();
}
//make another file with the full path AND the image this time, resized is a static string
File outputFile = new File(path+File.separator+resized);
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
注意,如果你犯同樣的錯誤,我沒有開始,你可能需要去到您的存儲和手動刪除該目錄與
有時畫廊需要刷新。通過ddms轉到您的文件夾路徑,並檢查您的新文件是否已創建。 – sachy
是的,我做到了多次withno運氣 – Brian
對於掃描新文件中看到這個[文章] [1] [1]:http://stackoverflow.com/questions/4646913/android-how-to -use-mediascannerconnection-scanfile/5815005#5815005 – sachy