【发布时间】:2015-12-27 07:54:13
【问题描述】:
我有一个简单的 Asynctask 代码,它是在从数据库加载后设置和图像到 imageview。我在我的片段的 onCreateView 中调用此类,但图像不在 imageview 中。 注意 - 该表只有一行,所以我认为查询没有问题。
protected class setpro extends AsyncTask<String, String, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
//Set profile
DataBaseOperations hell = new DataBaseOperations(getActivity());
SQLiteDatabase db = hell.getReadableDatabase();
String[] columns = {mDatabase.Tableinfo.Pic};
Cursor cur = db.query(mDatabase.Tableinfo.Table_Name, columns, null, null, null, null, null);
byte[] b = null;
Bitmap bp = null;
while (cur.moveToNext()) {
b = cur.getBlob(cur.getColumnIndex(mDatabase.Tableinfo.Pic));
}
if (b.length > 0) {
bp = BitmapFactory.decodeByteArray(b, 0, b.length);
cur.close();
db.close();
hell.close();
}
return bp;
}
@Override
protected void onPostExecute(Bitmap result) {
if(result != null){
ImageView iv = (ImageView) mview.findViewById(R.id.pro);
iv.setImageBitmap(result);
}
}
这是我的调用代码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mview = inflater.inflate(R.layout.fragment_profile, container, false);
Button Setprofile = (Button) mview.findViewById(R.id.setprofile);
// set profile pic
new setpro().execute("whatever");
}
【问题讨论】:
-
是
Bitmap result非空吗?在此处添加 logcat 语句以告诉您传入的内容
标签: android sqlite android-asynctask