简体   繁体   English

SQLite的ImageView和TextView的SimpleCursorAdapter

[英]SimpleCursorAdapter with ImageView and TextView from SQLite

I want to do an Phonebook, "name" Save names, "sname" Save phone, "img" to store pictures, But, the name and telephone can be displayed,iamge is stored using BASE64,Picture how to deal with? 我想做一个电话本,“名称”保存名字,“ sname”保存电话,“ img”保存图片,但是,名字和电话可以显示,图片是用BASE64存储的,图片如何处理?


@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): FATAL EXCEPTION: main 11-01 22:17:50.051: E/AndroidRuntime(4766): Process: com.example.electronicard, PID: 4766 11-01 22:17:50.051: E/AndroidRuntime(4766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.electronicard/com.example.electronicard.Menu}: java.lang.IllegalArgumentException: column 'img' does not exist 11-01 22:17:50.051: E/AndroidRuntime(4766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: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 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.electronicard.Menu}:java.lang.IllegalArgumentException:“ img”列不存在11-01 22:17:50.051:E / AndroidRuntime(4766):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)11-01 22:17:50.051:E / AndroidRuntime(4766):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)11-01 22:17:50.051:E / AndroidRuntime(4766):在android.app.ActivityThread.access $ 800(ActivityThread.java:156)11-01 22:17:50.051:E / AndroidRuntime(4766):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1355)11-01 22:17:50.051:E / AndroidRuntime(4766):在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): at 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): Caused by: java.lang.IllegalArgumentException: column 'img' does not exist 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.SimpleC ):在android.os.Looper.loop(Looper.java:157)11-01 22:17:50.051:E / AndroidRuntime(4766):在android.app.ActivityThread.main(ActivityThread.java:5883)11- 01 22:17:50.051:E / AndroidRuntime(4766):at java.lang.reflect.Method.invokeNative(本机方法)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):在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(本机方法)11-01 22:17:50.051:E / AndroidRuntime(4766):原因:java.lang.IllegalArgumentException:列'img'不存在11-01 22:17: 50.051:E / AndroidRuntime(4766):位于android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:309)11-01 22:17:50.051:E / AndroidRuntime(4766):位于android.support.v4.widget.SimpleC ursorAdapter.findColumns(SimpleCursorAdapter.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 more ursorAdapter.findColumns(SimpleCursorAdapter.java:317)11-01 22:17:50.051:E / AndroidRuntime(4766):位于android.support.v4.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:92)11-01 22:17 :50.051:E / AndroidRuntime(4766):位于com.example.electronicard.Menu.onCreate(Menu.java:59)11-01 22:17:50.051:E / AndroidRuntime(4766):位于android.app.Activity。 performCreate(Activity.java:5312)11-01 22:17:50.051:E / AndroidRuntime(4766):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)11-01 22:17:50.051:E / AndroidRuntime(4766):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)11-01 22:17:50.051:E / AndroidRuntime(4766):...还有11个

Your database stores image data BASE64 encoded, you have to decode it into the equivalent byte[] data before BitmapFactory can use it. 您的数据库存储了以BASE64编码的图像数据,您必须先将其解码为等效的byte []数据,然后BitmapFactory才能使用它。 BASE64 decoding is not the same as cc.getBytes() - that just gives you the byte values of the characters in the string. BASE64解码与cc.getBytes() -只是为您提供字符串中字符的字节值。

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

is the correct way (maybe with a different flag ). 是正确的方法(也许使用其他标志 )。

I would not do that every time you access the image data, storing it as already decoded binary BLOB in the database (or if data is > 100kB as actual file on SD) is far more efficient with regards to space and speed. 我不会每次访问图像数据时都这样做,将其作为已解码的二进制BLOB存储在数据库中(或者如果数据大于SD上的实际文件> 100kB)在空间和速度方面要高效得多。 http://androidsurya.blogspot.de/2012/11/insert-and-retrieve-image-from-sqlite.html looks like a usable example. http://androidsurya.blogspot.de/2012/11/insert-and-retrieve-image-from-sqlite.html看起来像一个可用的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM