简体   繁体   中英

blob data not set as bitmap on imageview android

i have insert blob data in to sqlite and retrive from sqlite that time blob data not set as bitmap on imageview. give msg to fail to load. so please help me for my code. following are my code.

            bitmap = android.provider.MediaStore.Images.Media
             .getBitmap(cr, selectedImage);

            imageView.setImageBitmap(bitmap);


            ByteArrayOutputStream stream= new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte imginbyte[]=stream.toByteArray();
            byte temp[]=imginbyte;

            myDb.execSQL("INSERT INTO tableimage VALUES('"+temp+"');");
            Cursor c= myDb.rawQuery("SELECT * FROM tableimage", null);
            c.moveToLast();
            byte asd[]= c.getBlob(c.getColumnIndex("imgpath"));
            byte img[]=null;

          //  BitmapFactory.decodeByteArray(asd, 0, asd.length);
            ImageView iv=(ImageView)findViewById(R.id.img2);
            ImageView iv1=(ImageView)findViewById(R.id.img3);

            iv.setImageBitmap( BitmapFactory.decodeByteArray(asd, 0, asd.length));
            iv1.setImageBitmap( BitmapFactory.decodeByteArray(asd, 0, asd.length));

Try this

ByteArrayInputStream imageStreamFront = new ByteArrayInputStream(asd);

Bitmap bitFrontImage = BitmapFactory.decodeStream(imageStreamFront);

if(bitFrontImage!=null)
{
   iv.setImageBitmap(bitFrontImage, 40);
}

Without blob store image and retrive. with string database see below code

bitmap = android.provider.MediaStore.Images.Media
             .getBitmap(cr, selectedImage);
            imageView.setImageBitmap(bitmap);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);

            byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
            Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
                    bytarray.length);

            myDb.execSQL("INSERT INTO imgtbl VALUES('"+encodedImageString+"');");
            Cursor c= myDb.rawQuery("SELECT * FROM imgtbl", null);

           c.moveToFirst();
           String img=c.getString(0);
           byte[] byarray = Base64.decode(img, Base64.DEFAULT);
           Bitmap bmimg = BitmapFactory.decodeByteArray(byarray, 0,
                   byarray.length);

            ImageView iv=(ImageView)findViewById(R.id.img2);
            ImageView iv1=(ImageView)findViewById(R.id.img3);

            iv.setImageBitmap(bmimg);
            iv1.setImageBitmap(bmimg);

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