我已经有一个图像存储在 BLOB 的数据库中,但我无法在布局中加载它,请帮助我解决问题。
应用程序在运行期间关闭(不幸的是 Image 已停止)。
MainActivity.java
package com.example.image;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
SQLiteDatabase myDB;
String TableName = "employee_details";
String Data = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = this.openOrCreateDatabase("hello", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery("SELECT image FROM employee_details WHERE name= 'john'", null);
byte[] blob = c.getBlob(c.getColumnIndex("image"));
Bitmap theImage = BitmapFactory.decodeByteArray(blob, 0, blob.length);
((ImageView)findViewById(R.id.item_icon)).setImageBitmap(theImage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
活动主
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
/>
我用于插入数据库的代码
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
myDB = this.openOrCreateDatabase("hello", MODE_PRIVATE, null);
myDB.execSQL("INSERT INTO " + TableName + " (name,image)" + " VALUES ('"
+ "john" + "','"+ bitmapdata + "');");
日志猫
08-01 09:12:57.375: E/Trace(28173): error opening trace file: No such file or directory (2)
08-01 09:12:58.816: D/AndroidRuntime(28173): Shutting down VM
08-01 09:12:58.816: W/dalvikvm(28173): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 09:12:58.825: E/AndroidRuntime(28173): FATAL EXCEPTION: main
08-01 09:12:58.825: E/AndroidRuntime(28173): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.image/com.example.image.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.os.Looper.loop(Looper.java:137)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 09:12:58.825: E/AndroidRuntime(28173): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 09:12:58.825: E/AndroidRuntime(28173): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 09:12:58.825: E/AndroidRuntime(28173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 09:12:58.825: E/AndroidRuntime(28173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 09:12:58.825: E/AndroidRuntime(28173): at dalvik.system.NativeStart.main(Native Method)
08-01 09:12:58.825: E/AndroidRuntime(28173): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:44)
08-01 09:12:58.825: E/AndroidRuntime(28173): at com.example.image.MainActivity.onCreate(MainActivity.java:32)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.Activity.performCreate(Activity.java:5104)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-01 09:12:58.825: E/AndroidRuntime(28173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-01 09:12:58.825: E/AndroidRuntime(28173): ... 11 more
有人可以向我发布从数据库中插入和检索图像的代码吗?如果您可以使用 Java 和 XML 发布整个代码,那将会很有帮助。