从 Sqlite 数据库访问图像

发布于 2024-10-26 19:12:38 字数 1886 浏览 0 评论 0原文

我的代码中有一个问题,我正在存储访问来自 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 变量始终分配为 null.........

I have a problem in my code that I am storing accessing the student data from sqlite database with student name, age and picture. I am storing all these data perfectly but cannot access the image field only.

Image shows blank on the screen,

I don't know why. Please Review my code and give me useful suggestions.

Thank You..

Here is Code:

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 variable always assigned as null.........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-11-02 19:12:38

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

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

我认为这会对您有所帮助。

Convert the byte[] bb to a ByteArrayInputStream. For your program, that would be

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

I think this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文