2012-12-27 8 views
16

मैं बाहरी संग्रहण में बिटमैप सहेजने के लिए इस कोड का उपयोग कर रहा है, लेकिन यह फ़ोल्डर बना नहीं है अगर यह नहीं मौजूद है:एंड्रॉयड में सहेजें बिटमैप बाहरी संग्रहण में जेपीईजी जैसे किसी फ़ोल्डर में

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

कोशिश यह कहीं भी होगी यू यकीन है कि परिणाम देता है::

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; 
     } 
संबंधित मुद्दे