如何从图像视图中获取图像?

时间:2012-06-04 14:03:48

标签: android sql database image

我正在尝试将图像放入图像视图中。这是我的方法:

// 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);
}

当我尝试它时,应用程序崩溃了。我认为我的陈述有点错误。任何想法???

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

Drawable myDrawable = mImageView.getDrawable();

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

现在您将位图转换为流并存储在db。