【问题标题】:Aceessing Image from Sqlite Database从 Sqlite 数据库访问图像
【发布时间】:2011-07-21 21:38:24
【问题描述】:

我的代码中有一个问题,我正在存储从 sqlite 数据库访问学生数据的学生姓名、年龄和图片。我完美地存储了所有这些数据,但不能只访问图像字段。

图像在屏幕上显示为空白,

我不知道为什么。请检查我的代码并给我有用的建议。

谢谢你..

这是代码:

package com.ex.imageStore;

import java.util.Locale;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class NewScreen extends Activity{
      SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newscreen);
        TextView name = (TextView)findViewById(R.id.textView1);
        TextView age = (TextView)findViewById(R.id.textView2);
        ImageView img = (ImageView)findViewById(R.id.imageView1);

        db = openOrCreateDatabase(
                "StudentData.db"
                , SQLiteDatabase.CREATE_IF_NECESSARY
                , null
                );

         db.setVersion(1);
            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);

            Cursor cur = db.query("stuData", 
                    null, null, null, null, null, null);
                //cur.moveToFirst();
            cur.moveToPosition(0);
            name.setText(cur.getString(1));
            age.setText(cur.getString(2));
            byte[] bb = cur.getBlob(3);
 Bitmap theImage = BitmapFactory.decodeByteArray(bb,0,bb.length);
            img.setImageBitmap(theImage);//-->It //cannot convert bytearray to bitmap
               /* while (cur.isAfterLast() == false) {
                    tv.append("n" + cur.getString(1));
                    cur.moveToNext();
                }*/
                cur.close();


    }

}

theImage 变量始终赋值为空.........

【问题讨论】:

  • 如何确保图像被保存在数据库中?

标签: android image sqlite


【解决方案1】:

byte[] bb 转换为ByteArrayInputStream。对于您的程序,那将是

ByteArrayInputStream is = new ByteArrayInputStream(bb);
Bitmap theImage = BitmapFactory.decodeStream(is);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setImageBitmap(theImage);

我想这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2016-12-10
    • 2014-06-02
    • 1970-01-01
    • 2013-03-20
    • 2019-01-16
    • 2010-12-05
    • 1970-01-01
    • 2017-06-05
    相关资源
    最近更新 更多