I have two functions for handling images. One is to convert from bitmap to byte[] and insert into database, the other is to convert byte[] to bitmap.
public Bitmap convertByteToBitmap(byte[] value){
byte[] new_value = Base64.decode(value, 0);
return BitmapFactory.decodeByteArray(new_value, 0, new_value.length);
}
public byte[] convertBitmapToByte(Bitmap img){
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.PNG, 100, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
return b;
}
I insert into the database with the following function
public void add_customer(int customer_id, byte[] image){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("customer_id", customer_id);
values.put("image", image);
db.insert("customers", null, values);
db.close();
}
And I call that function this way
add_customer(1, convertBitmapToByte(theBitmap));
I get the following error:
11-26 06:06:46.942: E/AndroidRuntime(7786): FATAL EXCEPTION: main
11-26 06:06:46.942: E/AndroidRuntime(7786): java.lang.RuntimeException: Unable to resume activity {com.myproject/main.myproject.printit}: java.lang.IllegalArgumentException: bad base-64
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.os.Looper.loop(Looper.java:137)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-26 06:06:46.942: E/AndroidRuntime(7786): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 06:06:46.942: E/AndroidRuntime(7786): at java.lang.reflect.Method.invoke(Method.java:525)
11-26 06:06:46.942: E/AndroidRuntime(7786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-26 06:06:46.942: E/AndroidRuntime(7786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-26 06:06:46.942: E/AndroidRuntime(7786): at dalvik.system.NativeStart.main(Native Method)
11-26 06:06:46.942: E/AndroidRuntime(7786): Caused by: java.lang.IllegalArgumentException: bad base-64
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.util.Base64.decode(Base64.java:161)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.util.Base64.decode(Base64.java:136)
11-26 06:06:46.942: E/AndroidRuntime(7786): at main.myproject.printit.signall(printit.java:60)
11-26 06:06:46.942: E/AndroidRuntime(7786): at main.myproject.printit.onResume(printit.java:96)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.Activity.performResume(Activity.java:5211)
11-26 06:06:46.942: E/AndroidRuntime(7786): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
11-26 06:06:46.942: E/AndroidRuntime(7786): ... 10 more
Am I inserting wrongly in the database or the problem is on retrieving the content? Thanks.
Edit: Solved: The problem was on the Base64, that wasn't needed.
return BitmapFactory.decodeByteArray(cursor.getBlob(0), 0, cursor.getBlob(0).length);