android - 如何将存储在数据库中的图像作为字符串显示为真实图像?

标签 android sql database image bitmap

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

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

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

resolveUri failed on bad bitmap 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);

        }

    });

}

最佳答案

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

您将不得不使用 BitmapFactory 函数 decodeByArray:

.setImageBitmap(BitmapFactory.decodeByteArray

希望对你有帮助

关于android - 如何将存储在数据库中的图像作为字符串显示为真实图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9217450/

相关文章:

android - Kivy for Android 中的空文本标签

android - 如何将 View 发送到后面?如何以编程方式控制 z 顺序?

sql - 有没有办法找出提交 SQL 查询的用户?

sql - 使用 WHERE 子句和 DISTINCT ON

mysql - 在 mysql 上对数百万行进行 SUM() 和 GROUP BY

database - 如何在 CSV 中表示 EAV?

database - 如何在一个表中连接五个表?

android - Espresso 测试时如何从 Asset 文件夹中读取 json 文件?

android - Android 的 Box API(v1)不再工作了?

sql - SQL Server 2008中设置主键的快捷方式是什么?