2010-05-05 6 views
0

hai, 안녕하세요 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에서 실행하면 작동합니다.

하지만 안드로이드 1.5에서 실행하면 작동하지 않습니다.

내 응용 프로그램은 android 1.5입니다.

수정하여 코드를 보내주십시오.

감사합니다.

+1

'제발 정정 하셔서 코드를 보내주세요 .' 제발 $ 50 그리고 기꺼이 도와 드리겠습니다. – ariefbayu

답변

0

추가 할 이진 데이터가 많으면 행에 넣는 것이 좋지 않습니다. 그러나 행에 파일에 대한 링크를 삽입해야합니다. 다음 예와 그 관리 안드로이드 : 안드로이드 문서에서

예 :

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

+0

이 코드는 안드로이드 설명서에서 얻은 것입니다. 어느 부분에 설명이 필요합니까? 이 코드는 Activity 클래스에서 호출한다고 가정합니다. "getContentResolver()"메소드는 사용자 정의 ContentProvider를 구현하고 AndroidManifest.xml 파일에 선언해야합니다. ContentProvider에 대한 자세한 내용을 보려면 다음으로 이동하십시오. http://developer.android.com/guide/topics/providers/content-providers.html 감사합니다. – Guaido79

관련 문제