我已將BLOB中的圖像存儲在數據庫中,但我無法在佈局中加載它,請幫我解決問題。我無法在圖像視圖中加載存儲在SQLite數據庫中的圖像
應用程序在運行過程中被關閉(不幸的是圖像已停止)。
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;
}
}
activity_main
<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 + "');");
logcat的代碼
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發佈整個代碼將會很有幫助。
給我們烏爾logcat的 –
也許'ImageView的IV =((ImageView的)findViewById(R.id.item_icon) );'然後'iv..setImageBitmap(theImage);'對我來說就像propper方式? – g00dy
你不檢查遊標是否爲空(沒有人名爲John)。此外,你應該通過光標循環獲取所有條目(超過1約翰) –