2010-05-05 54 views
0

海, 我是新来的android。png在数据库中的图像存储和在android 1.5检索

我有问题。

这是我的代码,但它不会工作,问题是在视图粘结剂。

请纠正它。

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

'请更正并发送代码给我.'给我50美元,我会很乐意帮助你。 – ariefbayu 2011-12-31 10:32:48

回答

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

希望这有助于。

再见。

+0

请发送有关该示例的完整源代码。 – hany 2010-05-06 12:33:52

+0

这段代码,就像我说的是从Android文档中获得的。哪部分需要说明?该代码假设是从一个Activity类中调用的。 “getContentResolver()”方法需要实现您自己的自定义ContentProvider,并在AndroidManifest.xml文件中声明它。要获取有关ContentProvider的更多信息,请转到此处:http://developer.android.com/guide/topics/providers/content-providers.html Regards – Guaido79 2010-05-16 19:11:53

相关问题