2015-07-04 135 views
2

我使用這段代碼保存圖像:保存圖像庫的Android

URL url = null; 
      try { 
       url = new URL("image"); 
      } catch (MalformedURLException e1) { 
       e1.printStackTrace(); 
      } 
      Bitmap bmp = null; 
      try { 
       bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
      OutputStream output; 
      File filepath = Environment.getExternalStorageDirectory(); 
      File dir = new File(filepath.getAbsolutePath() + "/folder name/"); 
      dir.mkdirs(); 
      File file = new File(dir, image + ".png"); 
      Toast.makeText(HomeActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show(); 
      try { 

       output = new FileOutputStream(file); 
       bmp.compress(Bitmap.CompressFormat.PNG, 100, output); 
       output.flush(); 
       output.close(); 

      } 

      catch (Exception e) { 
       e.printStackTrace(); 
      } 

當這個程式碼棒棒糖設備上運行的問題是,圖像不能在畫廊展示。我必須安裝文件管理器來檢查這些圖像。

有了這個代碼:

MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "image"; 

保存圖像的相機文件夾中。

我想在所有的Android設備中顯示具有特定文件夾名稱的圖庫中的圖像。

請幫忙。

+0

您需要在圖庫中添加圖像後刷新圖庫。 刷新庫: http://stackoverflow.com/questions/4144840/how-can-i-refresh-the-gallery-after-i-inserted-an-image-in-android –

+0

@VipulPatel:sendBroadcast( (Intent.ACTION_MEDIA_MOUNTED,Uri.parse (「file://」 + Environment.getExternalStorageDirectory()))); 文件是文件路徑嗎? – android

+0

你需要引用這個鏈接你的問題: http://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android –

回答

0

只要改變這些行,它會工作 -

File filepath = Environment.getExternalStorageDirectory().toString(); 
     File dir = new File(filepath + "/folder name/"); 
     dir.mkdirs(); 
     File file = new File(dir, image + ".png"); 
+0

沒有顯示在畫廊的專輯:( – android

9
public void saveImageToExternal(String imgName, Bitmap bm) throws IOException { 
//Create Path to save Image 
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder 
path.mkdirs(); 
File imageFile = new File(path, imgName+".png"); // Imagename.png 
FileOutputStream out = new FileOutputStream(imageFile); 
try{ 
    bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image 
    out.flush(); 
    out.close(); 

    // Tell the media scanner about the new file so that it is 
    // immediately available to the user. 
    MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() { 
     public void onScanCompleted(String path, Uri uri) { 
      Log.i("ExternalStorage", "Scanned " + path + ":"); 
      Log.i("ExternalStorage", "-> uri=" + uri); 
     } 
    }); 
} catch(Exception e) { 
    throw new IOException(); 
} 

}

爲我工作。 感謝您的時間

+0

後幾乎一個星期,終於有了解決方法。謝謝:) – Naila

0

如果你想在一個目錄中保存一個圖像,那麼這個代碼爲我工作!

saveImage(data); 

      private void saveImage(Bitmap data) { 
       File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test"); 
       if(!createFolder.exists()) 
       createFolder.mkdir(); 
       File saveImage = new File(createFolder,"downloadimage.jpg"); 
       try { 
        OutputStream outputStream = new FileOutputStream(saveImage); 
        data.compress(Bitmap.CompressFormat.JPEG,100,outputStream); 
        outputStream.flush(); 
        outputStream.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      }