閱讀96 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android 怎麼把imageview 轉為Bitmap

今天做一個應用,新增一筆記錄到sqlite中去,
記錄中有一個字段是一個圖像,在新增界麵中我用的是imageview控件,點擊彈出一個對話框,列出可供選擇的圖像,單擊選擇,但是提交後圖像顯示不正確,隻顯示一個黑框,我的代碼是這樣的

Java代碼 
  1. Bitmap image = Bitmap.createBitmap(imageView.getDrawable(). .....);
  2. 從imageview的Drawable裏取出信息構造一個bitmap,事實證明是行不通的。
   Bitmap image = Bitmap.createBitmap(imageView.getDrawable().  .....);
   從imageview的Drawable裏取出信息構造一個bitmap,事實證明是行不通的。

搜索了一下怎麼把ImageView轉換成Bitmap,發現網上提供一種方法:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
我試了一下,成功是成功了,但是得到的bitmap大小不對,原本很大的,放到列表裏顯示得超小,不是我想要的結果。
後來試了一下:
Java代碼
  1. Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

成功搞定。

最後更新:2017-04-02 17:28:39

  上一篇:go Service onStartCommand各種返回詳解,解決問題:隻調用onCreate不調用onStartCommand
  下一篇:go Hibernate下搭建JUNIT的測試環境,使用beforeclass和afterclass實現sessionFactory建立一次