无法从android SQLite数据库中检索图像

时间:2015-12-04 16:02:09

标签: android image sqlite

我可以将图像插入到sqlite数据库中 但无法检索图像 - 出现错误 当我调用检索功能时,它显示错误 Eclipse -Android应用代码Download: - http://www.filetolink.com/7efa8cc6dc

数据库

enter code here
package com.example.pricelist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHandler extends SQLiteOpenHelper {

    public static final String DB_NAME = "price.db";
    public static final String TABLE_NAME = "Details";
    public static final String IMAGE = "image";
    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String PRICE="price";
    public DataHandler(Context context) {
        super(context, DB_NAME, null, 1);
        @SuppressWarnings("unused")
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " TEXT," + PRICE + " REAL,"
                + IMAGE + " BLOB NULL) ");
}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public Boolean insert(ContentValues cv)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        long result = db.insert(TABLE_NAME, null, cv);
        if (result == -1)
            return false;
        else
            return true;
    }

    public Cursor get(String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String columns[] = { NAME };
        String selection = NAME + " LIKE ?";
        Cursor cu=db.query(TABLE_NAME, columns, selection, new String[] {"%"+ name+ "%" }, null, null,  NAME+" DESC");
        return cu;
    }
/*  public Cursor all()
    {
         String selectQuery = "SELECT  * FROM " + TABLE_NAME;
         SQLiteDatabase db = this.getReadableDatabase();
         Cursor cur = db.rawQuery("select id,name from Details", null);
        return cur;
    }*/
    public Cursor all()
    {
         SQLiteDatabase db = this.getReadableDatabase();
         String[] col={"image"};
         Cursor cur =db.query("Details", col, null, null, null, null, null);
         return cur;
    }
}

插入

    package com.example.pricelist;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Add extends Activity {
    static final int CAMERA_REQUEST = 1888;
    private Uri img;
    ImageView imgv;
    ContentValues cv;
    EditText price, name;
    Double val;
    Boolean status = false;
    DataHandler dh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        imgv = (ImageView) findViewById(R.id.imgv);
        price = (EditText) findViewById(R.id.price);
        name = (EditText) findViewById(R.id.name);
        cv = new ContentValues();
    }

    public void cam(View v) {
        Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File photo = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "pic.jpg");
        img = Uri.fromFile(photo);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, img);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {
            if (resultCode == RESULT_OK) {
                Bitmap takenImage = BitmapFactory.decodeFile(img.getPath());
                // Load the taken image into a preview
                imgv.setImageBitmap(takenImage);
                try {
                    FileInputStream fis = new FileInputStream(img.getPath());
                    byte[] image = new byte[fis.available()];
                    fis.read(image);
                    cv.put("name", name.getText().toString());
                    cv.put("price", Double.parseDouble(price.getText().toString()));
                    cv.put("image", image);
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } 
            else 
            { // Result was a failure
                Toast.makeText(this, "Picture wasn't taken!",Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void add(View v) {
        dh = new DataHandler(this);
        Boolean success = dh.insert(cv);
        if (success.equals(true)) 
        {
            Toast.makeText(Add.this, "Inserted Succesfully", Toast.LENGTH_SHORT).show();
            Intent in = new Intent(Add.this, MainActivity.class);
            startActivity(in);
        } 
        else 
        {
            Toast.makeText(Add.this, "Error", Toast.LENGTH_SHORT).show();
        }
    }

}

从数据库中检索图像

package com.example.pricelist;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class Image extends Activity {
    ImageView iv;
    DataHandler dh;
    Cursor cu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);
        iv = (ImageView) findViewById(R.id.imageviewdummy);
        dh = new DataHandler(this);
        cu=dh.all();
        byte[] byteArray = cu.getBlob(1);  
        Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0 ,byteArray.length);
        iv.setImageBitmap(bm);
    }

}

0 个答案:

没有答案