在我的应用程序中,我调用股票相机应用程序来拍照。在我的活动结果中,我启动了一个异步任务来处理照片。(旋转并上传照片)。
我还想从图库中删除照片。为此,我在异步任务中执行以下代码。
// Delete image from gallery
String[] imageColumns = { MediaStore.Images.Media._ID };
String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
String imageWhere = MediaStore.Images.Media._ID + ">?";
String[] imageArguments = { Integer.toString(captureLastID) };
CursorLoader imageLoader = new CursorLoader(mActivity);
imageLoader.setUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageLoader.setProjection(imageColumns);
imageLoader.setSelection(imageWhere);
imageLoader.setSelectionArgs(imageArguments);
imageLoader.setSortOrder(imageOrderBy);
Cursor imageCursor = imageLoader.loadInBackground();
if (imageCursor.getCount() > 0) {
while (imageCursor.moveToNext()) {
int id = imageCursor.getInt(imageCursor
.getColumnIndex(MediaStore.Images.Media._ID));
ContentResolver cr = mActivity.getContentResolver();
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media._ID + "=?",
new String[] { Long.toString(id) });
break;
}
}
imageCursor.close();
我得到错误
无法在未调用 Looper.prepare 的线程内创建处理程序
是什么导致了这个错误,我该如何解决?这是否与我调用 loadInBackground 的事实有关,而 asynctask 已经在后台运行?
这是我的日志:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.support.v4.content.Loader$ForceLoadContentObserver.<init>(Loader.java:52)
at android.support.v4.content.CursorLoader.<init>(CursorLoader.java:96)
at tasks.ProcessPictureTask.doInBackground(ProcessPictureTask.java:78)
at tasks.ProcessPictureTask.doInBackground(ProcessPictureTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.support.v4.content.Loader$ForceLoadContentObserver.<init>(Loader.java:52)
at android.support.v4.content.CursorLoader.<init>(CursorLoader.java:96)
at tasks.ProcessPictureTask.doInBackground(ProcessPictureTask.java:78)
at tasks.ProcessPictureTask.doInBackground(ProcessPictureTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)