【发布时间】:2014-04-14 19:45:46
【问题描述】:
我有一个 android 应用程序,我需要在其中拍照并将其存储为字节数组(SQLite 中的 blob),以备将来使用并检索此图像并显示它。但它不会显示。我检查了创建表的方法,有BLOB类型,还检查了我正在使用cursor.getBlob()检索数据。
为了捕捉我跟进了本教程http://developer.android.com/guide/topics/media/camera.html exept 我的onActivityResult 看起来不同,因为我正在获取空数据(自然,因为我已经存储了该图片):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
InputStream iStream = getContentResolver().openInputStream(fileUri);
inputData = getBytes(iStream);
}
}
而inputData 是我在SQLite 数据库中存储的BLOB。请注意,方法 getBytes 来自 https://stackoverflow.com/a/10297073/2966401
这就是我从数据库返回图片的方式:
public byte[] getAttachment(int taskId) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_TASKS, new String[] { KEY_ATTACHMENT1 },
KEY_ID + "=?", new String[] { String.valueOf(taskId) },
null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
return cursor.getBlob(0);
}
我在这里调用它并填充到 ImageView:
ImageView imgView = (ImageView) findViewById(R.id.attachment);
byte b[] = db.getAttachment(task.id);
Bitmap bp = BitmapFactory.decodeByteArray(b, 0, b.length);
imgView.setImageBitmap(bp);
请注意,bp 不为空。
这是附件所在的布局,背景图片只是看它是否显示正确:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".TaskDetailActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ImageView
android:id="@+id/attachment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:src="@drawable/background" />
</RelativeLayout>
</ScrollView>
问题是它不会显示任何图片,我不知道我在这里做错了什么。我试图将它传递给新的 Activity,但它太大了。我也尝试根据提到的第一个教程将jpg 更改为png,没有区别。我也为 Dialog https://stackoverflow.com/a/6045066/2966401 尝试了这个建议 ..但它再次显示为空白。
【问题讨论】:
-
什么是
imgView,它是否附加到某些视图层次结构中?bp是非空的,即解码有效吗? -
添加声明,bp 不为空。有什么想法吗?
-
好的,附件ImageView所在的布局如何?
-
添加了布局,用
blobAsBytes尝试了如下所示的建议,没有奏效。 -
新闻:尝试缩小位图并显示出来,但很小。你认为它可以打开大尺寸吗?问题是,我无法将如此大的数据发送到另一个 Activity 来打开它。
标签: android sqlite bytearray blob