我想做一个电话簿,“名称”保存名称,“sname”保存手机,“img”存储图片,但是,可以显示名称和电话,iamge使用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):致命异常:主要 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:column '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):在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(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):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:309) 11-01 22:17:50.051:E / AndroidRuntime(4766):在android.support.v4.widget.SimpleCursorAdapter.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):at 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更多
答案 0 :(得分:0)
您的数据库存储BASE64编码的图像数据,您必须在BitmapFactory可以使用之前将其解码为等效的byte []数据。 BASE64解码与cc.getBytes()
不同 - 它只是为您提供字符串中字符的字节值。
byte[] aa = Base64.decode(cc, Base64.DEFAULT);
是正确的方法(可能有不同的flag)。
每次访问图像数据时都不会这样做,将其作为已解码的二进制BLOB
存储在数据库中(或者如果数据在SD上的实际文件大于100kB)则更有效率空间和速度。 http://androidsurya.blogspot.de/2012/11/insert-and-retrieve-image-from-sqlite.html看起来像一个可用的例子。