在加载所有列表项期间,我尝试在后台加载缩略图。我得到一个错误和程序崩溃。我不知道是什么问题。
(1)我得到列表项ID
String id = listIDs.get(position);
task.execute(id);
(2)加载缩略图的AsyncTack如下
//Lazy image update
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
dbHelper.open();
byte[] img_bytes = dbHelper.getImagebyIDnumber(params[0]);
bitmap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length);
dbHelper.close();
return bitmap;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
(3)数据库中获取byte[]
图像数据的事件
public byte[] getImagebyIDnumber(String idnum){
Cursor mCursor = null;
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE, KEY_PASSWORD,
KEY_LOCATIONAUTOUPDATE, KEY_OVERSPEED, KEY_MOVEMENT, KEY_GEOFENCE, KEY_TIMEZONE, KEY_AUTHORIZE, KEY_IMAGE, KEY_ACTIVATE, KEY_NUMOFUSE},
KEY_IDNUM + " like '%" + idnum + "%'", null, null, null, null, null);
byte[] img_bytes = mCursor.getBlob(13);
return img_bytes;
}
错误发生在后台循环中。我的红色logcat是
08-19 22:45:05.690: D/dalvikvm(29110): threadid=17: still suspended after undo (sc=1 dc=1)
08-19 22:45:05.690: D/dalvikvm(29110): threadid=18: still suspended after undo (sc=1 dc=1)
08-19 22:45:05.705: E/AndroidRuntime(29110): FATAL EXCEPTION: AsyncTask #2
08-19 22:45:05.705: E/AndroidRuntime(29110): java.lang.RuntimeException: An error occured while executing doInBackground()
08-19 22:45:05.705: E/AndroidRuntime(29110): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-19 22:45:05.705: E/AndroidRuntime(29110): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-19 22:45:05.705: E/AndroidRuntime(29110): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-19 22:45:05.705: E/AndroidRuntime(29110): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-19 22:45:05.705: E/AndroidRuntime(29110): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-19 22:45:05.705: E/AndroidRuntime(29110): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-19 22:45:05.705: E/AndroidRuntime(29110): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-19 22:45:05.705: E/AndroidRuntime(29110): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)