png图像存储在数据库中并在android 1.5中检索

时间:2010-05-05 13:37:52

标签: android

亥, 我是android的新手。

我有问题。

这是我的代码,但它不起作用,问题出在视图绑定器中。

请更正。

// this is my activity 

package com.android.Fruits2;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class Fruits2 extends ListActivity {


    private DBhelper mDB;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 //       setContentView(R.layout.main);
        mDB = new DBhelper(this);

           mDB.Reset();

            Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

            mDB.createPersonEntry(new PersonData(img, "Harsha", 24,"mca"));


            String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_AGE, mDB.KEY_STUDY};
            String   table   = mDB.PERSON_TABLE;

            Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);

            startManagingCursor(c);

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                    R.layout.data,
                    c,
                    new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_AGE, mDB.KEY_STUDY},
                    new int[] {R.id.img, R.id.name, R.id.age,R.id.study});   


            adapter.setViewBinder( new MyViewBinder());

            setListAdapter(adapter);
    }
}


//my viewbinder  

package com.android.Fruits2;

import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;

public class MyViewBinder implements SimpleCursorAdapter.ViewBinder {

    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
           if( (view instanceof ImageView)  ) {
                 ImageView iv = (ImageView) view;
                 byte[] img = cursor.getBlob(columnIndex);
                 iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length));
                 return true;
            }

        return false;
    }

}  

// data 

package com.android.Fruits2;

import android.graphics.Bitmap;

public class PersonData {
       private Bitmap bmp;
        private String name;
        private int age;
        private String study;

        public PersonData(Bitmap b, String n, int k, String v) {
            bmp = b;
            name = n;
            age = k;
            study = v;
        }

        public Bitmap getBitmap() { return bmp; }
        public String getName() { return name; }
        public int getAge() { return age; }
        public String getStudy() { return study; }

}

//dbhelper 


package com.android.Fruits2;


import java.io.ByteArrayOutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.provider.BaseColumns;

public class DBhelper {
    public static final String KEY_ID = BaseColumns._ID;
    public static final String KEY_NAME = "name";
    public static final String KEY_AGE = "age";
    public static final String KEY_STUDY = "study";
    public static final String KEY_IMG = "image";

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;  

    private static final String DATABASE_NAME = "PersonalDB";
    private static final int DATABASE_VERSION = 1;

    public static final String PERSON_TABLE = "Person";

    private static final String CREATE_PERSON_TABLE = "create table "+PERSON_TABLE+" ("
                                         +KEY_ID+" integer primary key autoincrement, "
                                         +KEY_IMG+" blob not null, "
                                         +KEY_NAME+" text not null , "
                                         +KEY_AGE+" integer not null, "
                                         +KEY_STUDY+" text not null);";

    private final Context mCtx;
    private boolean opened = false;


    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_PERSON_TABLE);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS "+PERSON_TABLE);
            onCreate(db);
        }
    }

    public void Reset() {
     openDB();
     mDbHelper.onUpgrade(this.mDb, 1, 1);
     closeDB();
    }

    public DBhelper(Context ctx) {
        mCtx = ctx;
        mDbHelper = new DatabaseHelper(mCtx);
    }

    private SQLiteDatabase openDB() {
        if(!opened)
            mDb = mDbHelper.getWritableDatabase();
        opened = true;
        return mDb;
    }

    public SQLiteDatabase getHandle() { return openDB(); }

    private void closeDB() {
        if(opened)
            mDbHelper.close();
        opened = false;
    }

    public void createPersonEntry(PersonData about) {
     openDB();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        about.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
        ContentValues cv = new ContentValues();
        cv.put(KEY_IMG, out.toByteArray());            
        cv.put(KEY_NAME, about.getName());
        cv.put(KEY_AGE, about.getAge());
        cv.put(KEY_STUDY, about.getStudy());
        mDb.insert(PERSON_TABLE, null, cv);
        closeDB();
    }



}


//data.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id = "@+id/img"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
       >
    </ImageView>

    <TextView
        android:id = "@+id/name"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:textSize="15dp"
        android:textColor="#ff0000" 
      >
    </TextView>

    <TextView

        android:id = "@+id/age"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:textSize="15dp"
        android:textColor="#ff0000" 
        />


    <TextView
        android:id = "@+id/study"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:textSize="15dp"
        android:textColor="#ff0000" 
 />


</LinearLayout>

当我在Android 1.6和2.1中运行它时,它可以工作。

但是当我在Android 1.5中运行时,无法正常工作。

我的应用程序是android 1.5。

请更正并将代码发送给我。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果要添加大量二进制数据,则放入行中并不好。但是你必须在行中插入一个文件的链接。 Android管理如下例子:

Android文档示例:

import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;

// Save the name and description of an image in a ContentValues map.  
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");

// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
    outStream.close();
} catch (Exception e) {
    Log.e(TAG, "exception while writing image", e);
}

希望得到这个帮助。

再见。