【问题标题】:Android image from sqlite won't show来自 sqlite 的 Android 图像不会显示
【发布时间】: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


【解决方案1】:

你可以这样使用

Blob ImgByte = _CreditDetails.getBlob("ImageName");

            Drawable image = null;
            int blobLength = (int) ImgByte.length();  
            byte[] blobAsBytes = ImgByte.getBytes(1, blobLength);
            //release the blob and free up memory. (since JDBC 4.0)
            ImgByte.free();
            image =  new BitmapDrawable(BitmapFactory.decodeByteArray(blobAsBytes, 0, blobAsBytes.length));
Imageview.setBackgroundDrawable(image);

试试这个花花公子..这个对你有帮助。

【讨论】:

  • 其实 Sqlite 已经返回字节数组,我不认为我必须使用 ResultSet 和 JDBC,如果我理解你的第一行的话,因为没有声明..
  • 是的,由于一些 null 异常,我无法使用 SQLite 或 _CreditDetails 解析 ResultSet,但我将您的建议与我从数据库中获取的字节数组相结合它没有用,又是空白的 ImageView。
【解决方案2】:

好的,试试这个兄弟。

ImageView imgView = (ImageView) findViewById(R.id.attachment); 
byte b[] = db.getAttachment(task.id);
Drawable image = null;
image =  new BitmapDrawable(BitmapFactory.decodeByteArray(b, 0,b.length));
Imageview.setBackgroundDrawable(image);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多