png image store in database and retrieve in android 1.5
- by hany
hai,
I am new to android.
I have problem.
This is my code but it will not work, the problem is in view binder.
Please correct it.
// 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>
When I run this in android 1.6 and 2.1, it works.
But when I run in android 1.5, not work.
My application is android 1.5.
Please correct and send code to me.
Thank you.