2014-07-17 38 views
1

所以我有這個奇怪的問題。 9/10次,它可以很好地工作,但是這10%根本無法工作。保存Android ImageView位圖

我想簡單地有一個ImageView,我可以放大和平移。這很好用,然後我嘗試將imageView保存到手機本身。這是大多數情況下它會工作的地方,但有時它會將圖像保存爲黑屏......而不是ImageView上實際顯示的內容。

這裏是我的拯救ImageView的代碼:

protected void saveZoomedImage(){ 

    //create ImageView Bitmap 
    RelativeLayout tableContent; 
    tableContent=(RelativeLayout)findViewById(R.id.imgHolder); 
    tableContent.measure(MeasureSpec.makeMeasureSpec(tableContent.getLayoutParams().width, MeasureSpec.EXACTLY), 
      MeasureSpec.makeMeasureSpec(tableContent.getLayoutParams().height,MeasureSpec.EXACTLY)); 
    tableContent.setDrawingCacheEnabled(true); 
    final Bitmap screenshot = Bitmap.createBitmap(tableContent.getDrawingCache()); 
    tableContent.setDrawingCacheEnabled(true); 

    //save file 
    File folder = new File(Environment.getExternalStorageDirectory() + "/thedir"); 
    if(!folder.exists()){ 
     folder.mkdir(); 
    } 
    String exsistingFileName=Environment.getExternalStorageDirectory().getAbsolutePath() + "/thedir"; 
    FileOutputStream fos = null; 
    try {   
     fos = new FileOutputStream(exsistingFileName + "/image.jpg"); 
     screenshot.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
     fos.flush(); 
     fos.close(); 

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

} 

它保存文件很好,但它只是好像它不捕獲圖像上的次數不多%年齡有些道理的。

+0

您可以調用'tableContent.setDrawingCacheEnabled(true);'兩次。第二次你的意思是假嗎? – CodeMonkey

+0

啊是的大聲笑 - 認爲可能導致了這個問題,但? – user3267847

+0

我不知道。這是可能的,我想。這是唯一突出給我的東西。如果您有第10次出現的錯誤消息,如果您編輯了文章以包含該文章,則可能會有所幫助。 – CodeMonkey

回答

0

試試這個代碼:

public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) { 
    String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_"); 
    String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg"; 
    try { 
     FileOutputStream fos = new FileOutputStream(new File(filePath)); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return filePath; 
} 
1

首先確保您的應用程序啓用了存儲許可:

轉至設備設置>設備>應用程序>應用程序管理器>「你的應用」>權限>啓用存儲權限!

權限清單中:

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

所以,如果你想在你的文件存儲到創建自己的目錄,你可以使用somethibng像:

String myDate = getCurrentDateAndTime(); 
FileOutputStream outStream = null; 
File sdCard = Environment.getExternalStorageDirectory(); 
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName"); 
dir.mkdirs(); 
String fileName = "YourFileName_"+myDate+".jpg"; 
File outFile = new File(dir, fileName); 
outStream = new FileOutputStream(outFile); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
outStream.flush(); 
outStream.close(); 
//to refresh the gallery 
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
intent.setData(Uri.fromFile(file)); 
sendBroadcast(intent); 

}

助手功能:

private String getCurrentDateAndTime() { 
Calendar c = Calendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); 
String formattedDate = df.format(c.getTime()); 
return formattedDate; 

希望這有助於!

+1

你能否描述你的答案更多 –

+0

編輯我的答案!祝你好運! –

+0

老兄,我在評論之前給了你投票。我們正試圖互相幫助。這就是全部 –