2014-10-29 85 views
0

我想做一個電話簿,「名稱」保存姓名,「sname」保存手機,「img」來存儲圖片,但是,名稱和電話可以顯示,iamge存儲使用BASE64,圖片如何處理?SimpleCursorAdapter與SQLite的ImageView和TextView


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_menu); 
    tv=(TextView)findViewById(R.id.textView1); 
    tv1=(TextView)findViewById(R.id.textView2); 
    Typeface face = Typeface.createFromAsset(getAssets(),"fonts/aaq.otf"); 
    tv.setTypeface(face); 
    edt1=(EditText)findViewById(R.id.editText1);  
    lv = (ListView)findViewById(R.id.listView1); 

    Toast a=Toast.makeText(getApplicationContext(),"Message Toast!", Toast.LENGTH_LONG); 
    a.setGravity(Gravity.CENTER , 0, 0); 
    a.show(); 
    db = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null); 
    try { 
     db.execSQL("DROP TABLE hotlist"); 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 

    cur=db.rawQuery("SELECT * FROM "+ TB_NAME, null); 
    adapter=new SimpleCursorAdapter(this, 
      R.layout.mylayout, cur, 
      FROM, 
      new int[] {R.id.textView11,R.id.textView22,R.id.imageView11}, 0); 
    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      if (view.getId() == R.id.imageView11) 
      { 
      String cc=cur.getString(cur.getColumnIndexOrThrow("img")); 
      byte[] aa=cc.getBytes(); 
      Bitmap bb=BitmapFactory.decodeByteArray(aa, 0,aa.length); 
       ((ImageView)view).setImageBitmap(bb); 
        return true;} 

      return false;} 
    }); 



} 

11-01 22:17:50.051:E/AndroidRuntime(4766):致命異常:主 11-01 22:17:50.051:E/AndroidRuntime(4766):過程:com.example.electronicard,PID:4766 11-01 22:17:50.051:E/AndroidRuntime(4766):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.electronicard/com.example。電子/安卓運行時間(4766):在android.app.ActivityThread.performLaunchActivity(ActivityThread.jav) a:2596) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653) 11-01 22:17:50.051:E/AndroidRuntime 4766):at android.app.ActivityThread.access $ 800(ActivityThread.java:156) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java :1355) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.os.Handler.dispatchMessage(Handler.java:102) 11-01 22:17:50.051:E/AndroidRuntime(4766) ):at android.os.Looper.loop(Looper.java:157) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.app.ActivityThread.main(ActivityThread.java:5883) 11-01 22:17:50.051:E/AndroidRuntime(4766):在java.lang.reflect.Method.invokeNative(Native Method) 11-01 22:17:50.051:E/AndroidRuntime(4766):at java.lang.reflect.Method.invoke(Method.java:515) 11-01 22:17:50.051:E/AndroidRuntime(4766) :at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:871) 11-01 22:17:50.051:E/AndroidRuntime(4766):at com.android.internal.os.ZygoteInit。 main(ZygoteInit.java:687) 11-01 22:17:50.051:E/AndroidRuntime(4766):at dalvik.system.NativeStart.main(Native Method) 11-01 22:17:50.051:E/AndroidRuntime (4766):引起:java.lang.IllegalArgumentException:列'img'不存在 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java: 309) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.support.v4.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapte r.java:317) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.support.v4.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:92) 11-01 22:17:50.051 :E/AndroidRuntime(4766):at com.example.electronicard.Menu.onCreate(Menu.java:59) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.app.Activity.performCreate (Activity.java:5312) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 11-01 22:17:50.051:E/AndroidRuntime(4766):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552) 11-01 22:17:50.051:E/AndroidRuntime(4766):... 11更多

回答

0

您的數據庫存儲圖像數據BASE64編碼在BitmapFactory可以使用它之前,你必須將它解碼成相應的byte []數據。 BASE64解碼與cc.getBytes()不同 - 它只是給出字符串中字符的字節值。

byte[] aa = Base64.decode(cc, Base64.DEFAULT); 

是正確的方法(可能與不同的flag)。

我不會在每次訪問圖像數據時將其存儲爲數據庫中的已解碼二進制BLOB(或者如果數據大於100kB,因爲SD上的實際文件)在空間和速度方面效率更高。 http://androidsurya.blogspot.de/2012/11/insert-and-retrieve-image-from-sqlite.html看起來像一個可用的例子。

+0

zapl,謝謝你!我試試吧! – 2014-10-30 15:23:04

+0

但是,你可以提供simplecursoradapter setviewbind的用法它,請 – 2014-10-30 15:56:12

+0

@KennyHuang,如果你只是用上面寫的東西替換'byte [] aa = cc.getBytes();'那麼它工作嗎?你的代碼看起來不錯。 – zapl 2014-10-30 17:16:59

相關問題