2012-12-27 59 views
16

我使用這段代碼保存在外部存儲位圖,但如果它不存在,它不會創建該文件夾:保存位圖在Android作爲JPEG在外部存儲設備的文件夾中

String path = Environment.getExternalStorageDirectory().toString(); 
     OutputStream fOutputStream = null; 
     File file = new File(path + "/Captures/", "screen.jpg"); 
     try { 
      fOutputStream = new FileOutputStream(file); 

      capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream); 

      fOutputStream.flush(); 
      fOutputStream.close(); 

      MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show(); 
      return; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

我如何保存新目錄中的圖像是否存在,如果文件夾存在於設備中,則保存默認值?

+0

file.getParentFile()。mkdirs() – njzk2

+0

看看如何做到這一點在的AsyncTask http://stackoverflow.com/a/29795857/3496570 – Nepster

回答

26

試試這個它會插上U結果肯定:

String root = Environment.getExternalStorageDirectory().toString(); 
File myDir = new File(root + "/req_images"); 
myDir.mkdirs(); 
Random generator = new Random(); 
int n = 10000; 
n = generator.nextInt(n); 
String fname = "Image-" + n + ".jpg"; 
File file = new File(myDir, fname); 
Log.i(TAG, "" + file); 
if (file.exists()) 
    file.delete(); 
try { 
    FileOutputStream out = new FileOutputStream(file); 
    bm.compress(Bitmap.CompressFormat.JPEG, 90, out); 
    out.flush(); 
    out.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

添加這個以顯示在圖庫中:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

看在這個鏈接清楚的答案: show folder images in gallery

+0

您的代碼爲我工作,但該文件夾和圖像不顯示在設備庫中。你有什麼主意嗎? –

+1

它會顯示在你的設備上,但如果你想在畫廊中顯示只是做這件事... –

2

使用以下命令:

File dir = new File(path + "/Captures/"); 
if(!dir.exists()) { 
    dir.mkdirs(); 
} 
File file = new File(path + "/Captures/", "screen.jpg"); 
...... 
9

請使用下面的代碼片段可能會幫助全

String path = Environment.getExternalStorageDirectory().toString(); 
    OutputStream fOutputStream = null; 
    File file = new File(path + "/Captures/", "screen.jpg"); 
    if (!file.exists()) { 
     file.mkdirs(); 
    } 

    try { 
     fOutputStream = new FileOutputStream(file); 

     capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream); 

     fOutputStream.flush(); 
     fOutputStream.close(); 

     MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show(); 
     return; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
1

遲了,但可能有助於某人。由於使用了BufferOutPutStream,因此使用下面的代碼可以更快地將位圖保存在外部目錄中。

public boolean storeImage(Bitmap imageData, String filename) { 
      // get path to external storage (SD card) 

      File sdIconStorageDir = null; 


      sdIconStorageDir = new File(Environment.getExternalStorageDirectory() 
        .getAbsolutePath() + "/myAppDir/"); 
      // create storage directories, if they don't exist 
if(!sdIconStorageDir.exist()) 
{ 
sdIconStorageDir.mkdirs(); 
}    
      try { 
       String filePath = sdIconStorageDir.toString() + File.separator + filename; 
       FileOutputStream fileOutputStream = new FileOutputStream(filePath); 
       BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
       //Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show(); 
       // choose another format if PNG doesn't suit you 
       imageData.compress(Bitmap.CompressFormat.PNG, 100, bos); 
       bos.flush(); 
       bos.close(); 


      } catch (FileNotFoundException e) { 
       Log.w("TAG", "Error saving image file: " + e.getMessage()); 
       return false; 
      } catch (IOException e) { 
       Log.w("TAG", "Error saving image file: " + e.getMessage()); 
       return false; 
      } 
      return true; 
     } 
相關問題