我有两个处理图像的功能。一种是从位图转换为byte []并插入数据库,另一种是将byte []转换为位图。
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;
}
我使用以下函数
插入数据库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();
}
我用这种方式调用这个函数
add_customer(1, convertBitmapToByte(theBitmap));
我收到以下错误:
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
我是否错误地插入数据库或问题是检索内容?感谢。
修改:已解决:问题出在Base64上,不需要。
return BitmapFactory.decodeByteArray(cursor.getBlob(0), 0, cursor.getBlob(0).length);