2013-06-18 44 views
1

我想保存的ImageView在特定的文件夾中的形象,我試試這個代碼,但它不工作保存的ImageView圖像在特定的文件夾

public void saveImage(View v){ 
     View content = findViewById(R.id.iv_photo); 
     content.setDrawingCacheEnabled(true); 
      Bitmap bitmap = content.getDrawingCache(); 
      //File file = new File("/DCIM/Camera/image.jpg"); 
      File root = Environment.getExternalStorageDirectory(); 
      File file = new File(root.getAbsolutePath() + "/DCIM/image.jpg"); 
      try { 
       file.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(file); 
       bitmap.compress(CompressFormat.JPEG, 100, ostream); 
       ostream.close(); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 

    } 

這個代碼調用函數

saveImage(getWindow().getDecorView().findViewById(android.R.id.content)); 
+0

我真的不明白你想達到什麼目的。你想從資源保存文件在SD卡或? – hardartcore

+0

@ Android-Developer是的,我有一個圖像庫的應用程序,我希望用戶可以將它保存到他自己的畫廊。 –

回答

1

要保存您的應用程序的資源的圖像文件,你可以這樣進行:

File dest = Environment.getExternalStorageDirectory(); 
InputStream in = context.getResources().getDrawable(R.drawable.my_image); 
// Used the File-constructor 
OutputStream out = new FileOutputStream(new File(dest, "myNewImage.png")); 

// Transfer bytes from in to out 
byte[] buf = new byte[1024]; 
int len; 
try { 
    // A little more explicit 
    while ((len = in.read(buf, 0, buf.length)) != -1){ 
     out.write(buf, 0, len); 
    } 
} finally { 
    // Ensure the Streams are closed: 
    in.close(); 
    out.close(); 
} 

,這應該做的T裏克。

+0

好的,這爲從「R.drawable.my_image」保存圖像,但我怎麼能從「R.id.imageView」保存 –

+0

爲什麼你需要保存在SD卡imageView的ID? – hardartcore

+0

,因爲我有我的應用程序中的選項從相機拍攝圖像,那麼用戶可以編輯顏色和許多其他選項,所有這些操作都是在imageview中完成的。 –

相關問題