2014-01-13 73 views
0

已經imagview希望將其保存到存儲器這裏是我的代碼:如何從ImageView保存圖像?

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


} 

保存沒有任何反應後,沒有圖像是存在嗎?

回答

1

試試這個..

變化root.createNewFile();cachePath.createNewFile();

File root = Environment.getExternalStorageDirectory(); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); 
try { 
    cachePath.createNewFile(); 
    FileOutputStream ostream = new FileOutputStream(cachePath); 
    bitmap.compress(CompressFormat.JPEG, 100, ostream); 
    ostream.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

編輯:

FileOutputStream ostream = new FileOutputStream(cachePath); 
+0

圖像保存,但它的不可讀像它curupted ? – Soheyl

+0

@Sheheyl你是否改變了這一點。如果不改變並嘗試'FileOutputStream ostream = new FileOutputStream(cachePath);' – Hariharan

0

如何使用下面的代碼片段?

Drawable d = imageView.getBackground(); 
Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); 

File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext"); 
outStream = new FileOutputStream(file); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
outStream.flush(); 
outStream.close(); 
1

使用此功能可在SD卡存儲:

private void SaveIamge(Bitmap finalBitmap) { 

    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/saved_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); 
    if (file.exists()) file.delete(); 
    try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

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

,並添加清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

對我來說非常適合!創建隨機圖像名稱並將其保存在SD卡上。大!謝謝你 – LikePod

0

1 - 你需要在amnifest適當的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

2-了out.flush()檢查出不爲空..

3 -

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
          "/yourforlderName"; 
    File dir = new File(file_path); 
if(!dir.exists()) 
    dir.mkdirs(); 
    File file = new File(dir, "yourforlderName" + image + ".png"); 
    FileOutputStream fOut = new FileOutputStream(file); 

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
    fOut.flush(); 
    fOut.close(); 

如果您不在SD卡中創建目錄,那麼如何將圖像存儲在特定位置的SD卡中? 所以請檢查這個...我希望它對你有用。

0

從ImageView的獲取位圖:

imageview.buildDrawingCache(); 
    Bitmap bm=imageview.getDrawingCache(); 

將它保存在一個文件中:

OutputStream fOut = null; 
    Uri outputFileUri; 
    try { 
    File root = new File(Environment.getExternalStorageDirectory() 
     + File.separator + "folder_name" + File.separator); 
    root.mkdirs(); 
    File sdImageMainDirectory = new File(root, "myPicName.jpg"); 
    outputFileUri = Uri.fromFile(sdImageMainDirectory); 
    fOut = new FileOutputStream(sdImageMainDirectory); 
    } catch (Exception e) { 
    Toast.makeText(this, "Error occured. Please try again later.", 
     Toast.LENGTH_SHORT).show(); 
    } 

    try { 
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
    fOut.flush(); 
    fOut.close(); 
    } catch (Exception e) { 
    } 

加入清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />