如何将图标存储到Blob中?
有人请帮我解决这个问题
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
Blob b;
我试过这个::
button1 = (Button)findViewById(R.id.button1);
try{
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
ImageView 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;
}
答案 0 :(得分:0)
无论如何,我看到你从Drawable转换为Bitmap,所以你可以使用它:
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);
}
从Bitmap转换为byteArray。您可以将其作为blob存储在数据库中:
Bitmap myBitmap; //should be initialized to some value
byte[] byteArray = getBase64Bytes(myBitmap);
为了完整起见,如何再次从byteArray转换为Bitmap
public Bitmap getImageFromBase64Blob(byte[] blob){
byte[] byteArray = Base64.decode(blob, Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);
return bitmap;
}