简体   繁体   中英

Android image from sqlite won't show

I have an android app, where I need to take a picture and store it as a byte array (blob in SQLite), for future purpouses and retrieving this image back and show it. But it won't show. I checked the method for creating table, there is BLOB type, also I checked that I am retrieving data with cursor.getBlob() .
For capturing I followed up this tutorial http://developer.android.com/guide/topics/media/camera.html exept my onActivityResult looks different, because I wass getting null data (naturally, because I stored that picture already):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CAMERA_REQUEST) {
            if (resultCode == RESULT_OK) {
                    InputStream iStream = getContentResolver().openInputStream(fileUri);
                    inputData = getBytes(iStream);
        }
    }  

and inputData is what am I storing in my SQLite database as a BLOB . Note that method getBytes is from https://stackoverflow.com/a/10297073/2966401
This is how I am returning picture from db:

public byte[] getAttachment(int taskId) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_TASKS, new String[] {  KEY_ATTACHMENT1 }, 
        KEY_ID + "=?", new String[] { String.valueOf(taskId) },
        null, null, null, null);
if (cursor != null)
    cursor.moveToFirst();
return cursor.getBlob(0);
}

And here I am calling it and filling into ImageView:

ImageView imgView = (ImageView) findViewById(R.id.attachment);    
byte b[] = db.getAttachment(task.id);
Bitmap bp = BitmapFactory.decodeByteArray(b, 0, b.length);
imgView.setImageBitmap(bp); 

Note that bp is not null. This is the layout where attachment is, that background image is just to see if it shows right:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".TaskDetailActivity" >   
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >   

        <ImageView
            android:id="@+id/attachment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignLeft="@+id/button3"
            android:layout_below="@+id/button3"
            android:src="@drawable/background" />
 </RelativeLayout>    
</ScrollView>

Problem is that it won't show any picture and I don't know what am I doing wrong here. I tried to pass it to new Activity, but it's too big. Also I tried to change jpg to png according to first tutorial mentioned, no difference. Also I tried this advice for Dialog https://stackoverflow.com/a/6045066/2966401 ..but again, it showed blank.

Just u can use like this

Blob ImgByte = _CreditDetails.getBlob("ImageName");

            Drawable image = null;
            int blobLength = (int) ImgByte.length();  
            byte[] blobAsBytes = ImgByte.getBytes(1, blobLength);
            //release the blob and free up memory. (since JDBC 4.0)
            ImgByte.free();
            image =  new BitmapDrawable(BitmapFactory.decodeByteArray(blobAsBytes, 0, blobAsBytes.length));
Imageview.setBackgroundDrawable(image);

Try this one dude..this one helpful for you.

OK Try this one bro.

ImageView imgView = (ImageView) findViewById(R.id.attachment); 
byte b[] = db.getAttachment(task.id);
Drawable image = null;
image =  new BitmapDrawable(BitmapFactory.decodeByteArray(b, 0,b.length));
Imageview.setBackgroundDrawable(image);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM