如何将数据库中存储的图像以字符串形式显示为真实图像?

发布于 2025-01-04 02:47:10 字数 2218 浏览 2 评论 0原文

如何检索存储在数据库中的图像作为真实图像,我使用了两种不同的方法从数据库中获取图像,但没有一个对我有用。

我有这个类,我试图从数据库中检索图像作为列表视图。

在编译项目时,我收到以下信息:

resolveUri 因位图 URI 错误而失败

这是我的课程之一:

public class ListProp extends ListActivity {

private static final String TAG = ListProp.class.getSimpleName();
private static final String TAG_PHOTOS = "photoThumbnailUrl";
private SQLiteDatabase database;
private CursorAdapter dataSource;
Cursor cursor;
private static final String fields[] = { "photoThumbnailUrl",
        BaseColumns._ID };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DBHelper helper = new DBHelper(this);
    database = helper.getReadableDatabase();
    database = helper.getWritableDatabase();
    Cursor data = database.query(DBHelper.TABLE, fields, null, null, null,
            null, null);

    dataSource = new SimpleCursorAdapter(this, R.layout.image, data,
            fields, new int[] { R.id.image1 });

    setListAdapter(dataSource);
    System.out.println(dataSource);
    database.close();

    // selecting single ListView item
    final ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            System.out.println("I'm fine here");

            ImageView image = (ImageView) findViewById(R.id.image1);

            // get it as a ByteArray
            while (cursor.moveToFirst())
                ;
            byte[] imageByteArray = cursor.getBlob(cursor
                    .getColumnIndex(DBHelper.C_PHOTOS));

            // convert it back to an image
            ByteArrayInputStream imageStream = new    
                            ByteArrayInputStream(imageByteArray);
            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
            Drawable d = new BitmapDrawable(bitmap);
            image.setImageResource(R.drawable.ic_launcher);
            cursor.close();
            lv.addView(image);

        }

    });

}

}

How do I retrieve images stored in my database as real images, I have used two different methods to get the images from the database but none works for me.

I have this class which I'm trying to retrieve the images from the database as listview.

Whilst compiling the project I'm receiving the following:

resolveUri failed on bad bitmap uri

This is one of my classes:

public class ListProp extends ListActivity {

private static final String TAG = ListProp.class.getSimpleName();
private static final String TAG_PHOTOS = "photoThumbnailUrl";
private SQLiteDatabase database;
private CursorAdapter dataSource;
Cursor cursor;
private static final String fields[] = { "photoThumbnailUrl",
        BaseColumns._ID };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DBHelper helper = new DBHelper(this);
    database = helper.getReadableDatabase();
    database = helper.getWritableDatabase();
    Cursor data = database.query(DBHelper.TABLE, fields, null, null, null,
            null, null);

    dataSource = new SimpleCursorAdapter(this, R.layout.image, data,
            fields, new int[] { R.id.image1 });

    setListAdapter(dataSource);
    System.out.println(dataSource);
    database.close();

    // selecting single ListView item
    final ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            System.out.println("I'm fine here");

            ImageView image = (ImageView) findViewById(R.id.image1);

            // get it as a ByteArray
            while (cursor.moveToFirst())
                ;
            byte[] imageByteArray = cursor.getBlob(cursor
                    .getColumnIndex(DBHelper.C_PHOTOS));

            // convert it back to an image
            ByteArrayInputStream imageStream = new    
                            ByteArrayInputStream(imageByteArray);
            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
            Drawable d = new BitmapDrawable(bitmap);
            image.setImageResource(R.drawable.ic_launcher);
            cursor.close();
            lv.addView(image);

        }

    });

}

}

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

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

发布评论

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

评论(1

っ左 2025-01-11 02:47:10

尝试按照本教程进行操作: https://web.archive.org/web/20200718180158/http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

您必须使用BitmapFactory函数decodeByArray:

.setImageBitmap(BitmapFactory.decodeByteArray

希望这有帮助

Try following this tutorial: https://web.archive.org/web/20200718180158/http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

You will have to use the BitmapFactory function decodeByArray:

.setImageBitmap(BitmapFactory.decodeByteArray

Hope this helps

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