【问题标题】:How can get an image from an image view?如何从图像视图中获取图像?
【发布时间】:2012-06-04 14:03:48
【问题描述】:

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

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

当我尝试它时,应用程序崩溃了。我认为我的陈述在某种程度上是错误的。有什么想法???

【问题讨论】:

  • 您能添加任何日志跟踪吗?更多,在数据库中添加图片是一个非常糟糕的主意
  • 是的,但你能看出任何逻辑问题吗??
  • 好的,但是是否可以从图像视图中获取图像,将其转换并存储为字符串?

标签: android sql database image


【解决方案1】:
Drawable drawable = mImageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
}

【讨论】:

  • 这就是我从图像视图中获取图像的方式??
  • 检查空对象,以避免NPE
【解决方案2】:
Drawable myDrawable = mImageView.getDrawable();

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

现在您将位图转换为流并存储在数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2012-10-12
    相关资源
    最近更新 更多