2012-12-27 29 views
16

Tôi đang sử dụng mã này để lưu Bitmap trong lưu trữ bên ngoài nhưng nó không tạo ra các thư mục nếu nó không tồn tại:Lưu Bitmap trong Android như JPEG trong Bộ nhớ ngoài trong một thư mục

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; 
     } 

Làm thế nào tôi có thể lưu hình ảnh trong thư mục mới nếu không tồn tại và lưu mặc định nếu thư mục có trong thiết bị?

+0

file.getParentFile(). Mkdirs() – njzk2

+0

thấy làm thế nào để làm điều đó trong AsyncTask http://stackoverflow.com/a/29795857/3496570 – Nepster

Trả lời

26

thử này nó sẽ cung cấp cho u kết quả chắc chắn:

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(); 
} 

thêm ảnh này để hiển thị trong thư viện:

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

xem tại liên kết này để có câu trả lời rõ ràng: show folder images in gallery

+0

code của bạn làm việc cho tôi nhưng thư mục và hình ảnh không được hiển thị trong thư viện Thiết bị. Bạn còn ý kiến ​​nào không? –

+1

nó sẽ hiển thị trong thiết bị u r nhưng nếu bạn muốn hiển thị trong thư viện chỉ cần làm điều này ... –

2

Sử dụng như sau:

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

Vui lòng sử dụng đoạn mã dưới đây có thể được giúp đỡ đầy đủ

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

muộn nhưng có thể hữu ích cho ai đó. sử dụng bên dưới mã nó sẽ lưu bitmap trong thư mục bên ngoài nhanh hơn vì 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; 
     } 
Các vấn đề liên quan