2012-10-19 35 views
4

我想寫一個圖像文件到公共圖庫文件夾中的特定目錄,但我不斷收到一個錯誤,我無法打開該文件,因爲它的一個目錄。我如何保存在外部存儲庫中的圖像在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); 

注意,如果你犯同樣的錯誤,我沒有開始,你可能需要去到您的存儲和手動刪除該目錄與

+0

有時畫廊需要刷新。通過ddms轉到您的文件夾路徑,並檢查您的新文件是否已創建。 – sachy

+0

是的,我做到了多次withno運氣 – Brian

+0

對於掃描新文件中看到這個[文章] [1] [1]:http://stackoverflow.com/questions/4646913/android-how-to -use-mediascannerconnection-scanfile/5815005#5815005 – sachy

回答

8

您正試圖寫入目錄而不是文件。 試試這個

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory; 

File outputDir= new File(path); 


outputDir.mkdirs(); 
File newFile = new File(path+"/"+"test.png"); 
FileOutputStream out = new FileOutputStream(newFile); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); 
+0

發生了什麼是在一個點我的代碼將完整路徑寫爲一個目錄。所以我有一個名爲「testing.png」的目錄比我試圖將位圖寫入該目錄。此方法有效 – Brian

0

用這樣的方式:

bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/mnt/sdcard/" + new Date().getTime() + ".jpg"));` 

Path : Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + file name

2

你的代碼是正確的,只有很少的需求變化如下,

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory; 

    // First Create Directory 
    File outputFile = new File(path); 
    outputFile.mkdirs(); 

    // Now Create File 
    outputFile = new File(path,"testing.png"); 
    FileOutputStream out = new FileOutputStream(outputFile); 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out); 

也不要」忘了給WRITE_EXTERNAL_ STORAGE權限在您的AndroidManifest.xml文件中。

1

如果在Android模擬器上工作時出現此錯誤;您需要在模擬器上啓用SD卡存儲。

0
public static String SaveImage(Bitmap finalBitmap) { 

     String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 
     File myDir = new File(root + "/FolderName"); 
     if(!myDir.exists()) 
      myDir.mkdirs(); 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     String fname = "Image-"+ n +".png"; 
     File file = new File (myDir, fname); 
     if (file.exists()) file.delete(); 
     try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return file.getAbsolutePath(); 
    } 
相關問題