2013-02-21 67 views

回答

0

感謝您的回答。最後我這樣做:

var imageView = FindViewById<ImageView>(Resource.Id.image); 
Bitmap bmp = ((BitmapDrawable)imageView.Drawable).Bitmap; 

ps:我有一個NullPointerException,因爲imageView是在另一個視圖。

1

工作你應該能夠獲得通過可繪製屬性訪問到指定的圖像。在訪問它之前一定要檢查null。

1

這是我使用它的一個工作示例。

  view.setDrawingCacheEnabled(true); 
     view.buildDrawingCache(); 
     Bitmap save = view.getDrawingCache(); 

其中viewImageView,如果你想保存。那麼這裏是我的救命方法從我的ImageView的觀點,並將其保存爲.PNG

void Save() { 
    if (null != view.getDrawable()) { 
     view.setDrawingCacheEnabled(true); 
     view.buildDrawingCache(); 
     save = view.getDrawingCache(); 
     final File myDir = new File(folder); 
     myDir.mkdirs(); 
     final Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     final String fname = "StyleMe-" + n + ".png"; 
      file = new File(myDir, fname); 
     if (file.exists()) 
      file.delete(); 
     try { 
      final FileOutputStream out = new FileOutputStream(file); 
      save.compress(Bitmap.CompressFormat.PNG, 100, out); 
      out.flush(); 
      out.close(); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
        Uri.parse("file://" 
          + Environment.getExternalStorageDirectory()))); 
      Toast.makeText(getApplication(), "Image Saved", 
        Toast.LENGTH_SHORT).show(); 
     } catch (final Exception e) { 
      Toast.makeText(getApplication(), 
        "Something Went Wrong check if you have Enough Memory", 
        Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     final Toast tst = Toast.makeText(getApplication(), 
       "Please Select An Image First", Toast.LENGTH_LONG); 
     tst.setGravity(Gravity.CENTER, 0, 0); 
     tst.show(); 
    } 
    view.setDrawingCacheEnabled(false); 
} 

,這裏是我的變量。

 String folder = "/sdcard/Pictures/StyleMe"; 
     static File file;