我已经在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;
}
}
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发布整个代码,将会很有帮助。
答案 0 :(得分:1)
尝试改变这一点:
ImageView imgdisplay;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgdisplay = (ImageView) findViewById(R.id.item_icon);
myDB = this.openOrCreateDatabase("hello", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery("SELECT image FROM employee_details WHERE name= 'john'", null);
if (c.getCount() > 0) {
c.moveToFirst();
do {
byte[] blob = c.getBlob(c.getColumnIndex("image"))
Bitmap b = BitmapFactory.decodeByteArray(blob , 0, blob .length);
imgdisplay.setImageBitmap(b);
} while (c.moveToNext());
}
c.close();
//close yourdatabase here }
用于调整位图对象的大小:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;}