2014-10-29 116 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