开发者

Conversion Blob in android

开发者 https://www.devze.com 2023-03-13 21:01 出处:网络
how can i store an icon into Blob? anyone please help me on this topic Drawable icon = p.applicationInfo.loadIcon(getPackageManager());

how can i store an icon into Blob?

anyone please help me on this topic

Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
Blob b;

I have try this ::

 button1 = (Button)findViewById(R.id.button1);
       try{
       ImageView iv = (ImageView) findViewById(R.id.splashImageView);
       ImageV开发者_StackOverflow中文版iew iv2 = (ImageView)findViewById(R.id.imageView1);

        Drawable d =iv.getBackground();
        BitmapDrawable bitDw = ((BitmapDrawable) d);
        Bitmap bitmap = bitDw.getBitmap();
        System.out.println(".....d....."+d);
        System.out.println("...bitDw...."+bitDw);
        System.out.println("....bitmap...."+bitmap);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        byte[] imageInByte = stream.toByteArray();
        System.out.println("imageInByte"+imageInByte);

        String s = imageInByte.toString();
        byte[] imageInByte2 = s.getBytes();
         Bitmap btmp = convertByteArrayToBitmap(imageInByte2);
        /*iv.setImageResource(R.drawable.icon);*/

        iv2.setImageBitmap(btmp);





       }catch (Exception e) {
           e.printStackTrace();
    }



        //   i1.insertDataUser("tableName",3,"appname","pname", "versionName", 2,"s", 2121);
       /* int  a = i1.GetUserData(getApplicationContext(), "tablename",1);

        System.out.println("a is "+a);

        System.out.println(" i1.app_id"+i1.app_name);
        System.out.println(" i1.app_id"+i1.pname);
        System.out.println(" i1.app_id"+i1.version_name);
        System.out.println(" i1.app_id"+i1.versionCode);
        System.out.println(" i1.app_id"+i1.date);
        System.out.println(" i1.app_id"+i1.icon);*/




}
public static Bitmap convertByteArrayToBitmap(
        byte[] byteArrayToBeCOnvertedIntoBitMap) {

    Bitmap bitMapImage = BitmapFactory.decodeByteArray(
            byteArrayToBeCOnvertedIntoBitMap, 0,
            byteArrayToBeCOnvertedIntoBitMap.length);
    return bitMapImage;
}


I see you convert from Drawable to Bitmap anyway, so you might use this:

import android.util.Base64;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;

//use this method
private byte[] getBase64Bytes(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    return Base64.encode(byteArray, Base64.DEFAULT);
}

Convert from Bitmap to byteArray. You can store this as a blob in your database:

Bitmap myBitmap; //should be initialized to some value
byte[] byteArray = getBase64Bytes(myBitmap);

For the sake of completeness, how to convert from byteArray to Bitmap again

 public Bitmap getImageFromBase64Blob(byte[] blob){
        byte[] byteArray = Base64.decode(blob, Base64.DEFAULT);
        Bitmap bitmap= BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);
        return bitmap;
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号