我无法在imageview中加载存储在SQLite数据库中的图像 [英] I can't get to load an image stored in a SQLite database in imageview

查看:109
本文介绍了我无法在imageview中加载存储在SQLite数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在BLOB中的数据库中存储了一个图像,但我无法在布局中加载它,请帮助我解决问题。

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.

应用程序获取在运行期间关闭(不幸的是Image停止了)。

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

MainActivity.java

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

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

有人可以发给我插入和检索数据库图像的代码吗?如果您可以使用Java和XML发布整个代码将会很有帮助。

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.

推荐答案

尝试更改此内容:

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

这篇关于我无法在imageview中加载存储在SQLite数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆