2012-06-04 68 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(); 

现在你转换成位图流和店分贝。

相关问题