2

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);

        }

    });

}

}

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.