2012-01-31 67 views

回答

17

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) { 
} 
+0

謝謝你讓我試試這個.. .. @Seshu Vinay – 2012-02-01 06:42:23

+0

嘿vinay我收到錯誤消息「發生錯誤,請稍後再試。」甚至無法看到在根目錄下創建的任何目錄? – 2012-02-01 06:57:43

+3

在清單中添加權限: 2012-02-01 09:28:02

-1

首先獲取imageView的drawingCache(位圖),然後將位圖保存到SDCard。

File folder = new File(Environment.getExternalStorageDirectory()+「/ folder /」);如果(!folder.exists())folderAppointment.mkdirs();

try { 
    this.setDrawingCacheEnabled(true); 
    FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file")); 
    Bitmap bitmap = YOUR_IMAGE_VIEW.getDrawingCache(); 
    bitmap.compress(CompressFormat.JPEG, 100, fos); 
    fos.flush(); 
    fos.close(); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

謝謝你讓我試試這個.... @akkilis – 2012-02-01 06:42:52

2

你必須

  1. 保存圖像到你的持久化存儲。
  2. 添加條目到MediaStore內容提供商。

第一個可以使用下面的代碼來實現:

FileOutputStream out = new FileOutputStream(filePath); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 

其次,

MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description); 
+0

謝謝讓我試試.... @Ragunath Jawahar – 2012-02-01 06:43:38