2012-06-04 67 views
-1

我想獲得一個圖像,我把它放入圖像視圖。這是我的方法:如何從圖像視圖獲取圖像?

// Get the image in the image view 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    Bitmap myBitmap = mImageView.getDrawingCache(); 
    myBitmap.compress(CompressFormat.PNG, 0, outputStream); 

然後我想將它插入到數據庫:

mDbHelper.createReminder(outputStream); 

的DatabaseAdapter看起來是這樣的:

public long createReminder(ByteArrayOutputStream outputStream) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_IMAGE, outputStream.toByteArray());   
    return mDb.insert(DATABASE_TABLE, null, initialValues); 
} 

當我嘗試它的應用程序崩潰。我認爲我的發言有些不妥。有任何想法嗎???

+1

你可以添加任何日誌跟蹤? 更多,在數據庫中添加圖片是一個非常糟糕的主意 – VinceFR

+0

是的,但你可以看到任何邏輯問題? – user1420042

+0

好的,但是有可能從圖像視圖中獲取圖像,將其轉換並存儲爲字符串? – user1420042

回答

1
Drawable drawable = mImageView.getDrawable(); 
if (drawable instanceof BitmapDrawable) { 
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; 
    Bitmap bitmap = bitmapDrawable.getBitmap(); 
} 
+0

這是我如何從圖像視圖中獲取圖像? – user1420042

+0

是的,它是....... – Blackbelt

+0

檢查空對象,以避免NPE – Blackbelt

1
Drawable myDrawable = mImageView.getDrawable(); 

Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap(); 

現在你轉換成位圖流和店分貝。

相關問題