I can't get to load an image stored in a SQLite database in imageview

ぃ、小莉子 提交于 2019-12-11 22:37:55

问题


I already have an image stored in the database in BLOB but I can't load it in an layout, please help me with the problem.

The application gets closed during its running (unfortunately Image has stopped).

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"
   />

The code I used for inserting into database

  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

Can someone post me the codes for inserting and retrieving image from database? It would be helpful if you could post the whole code with Java and XML.


回答1:


Try to change this:

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 }

for resize your bitmap object:

 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;}


来源:https://stackoverflow.com/questions/17988929/i-cant-get-to-load-an-image-stored-in-a-sqlite-database-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!