实现AsyncTask:执行doInBackground()时发生错误会引发运行时异常

时间:2015-11-23 11:23:22

标签: java android android-asynctask

我正在尝试实现AsyncTak,以便在拍摄照片时通知用户正在运行后台操作。 AyncTask能够显示setMessage函数,但应用程序崩溃并显示以下错误

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

这是我的AsyncTask的完整代码库

private class CheckTypesTask extends AsyncTask<Void, Void, Void>{
             ProgressDialog asyncDialog = new ProgressDialog(MainActivity.this);


             @Override
             protected void onPreExecute() {
                 //set message of the dialog
                 asyncDialog.setMessage("Loading");
                 //show dialog
                 asyncDialog.show();
                 super.onPreExecute();
             }

             @Override
             protected Void doInBackground(Void... arg0) {

                 //don't touch dialog here it'll break the application
                 //do some lengthy stuff like calling login webservice
                 onPhotoTaken();

                 return null;
             }

             @Override
             protected void onPostExecute(Void result) {
                 //hide the dialog
                 asyncDialog.dismiss();

                 super.onPostExecute(result);
             }

     }

然后我以这种方式调用onActivity结果中的AsyncTask:

if (resultCode == -1) {

            //onPhotoTaken();
            (new MainActivity.CheckTypesTask()).execute();

        } else {
            Log.v(TAG, "User cancelled");
        }
    }

EDITTED: 这是onPhotoTaken代码,它在MainActivity类中定义。

public void onPhotoTaken() {

        _taken = true;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(_path, options);

        try {
            //ProgressDialog dialog = ProgressDialog.show(this, "Loading", "Please wait...", true);
            ExifInterface exif = new ExifInterface(_path);
            int exifOrientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            Log.v(TAG, "Orient: " + exifOrientation);

            int rotate = 0;

            switch (exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            }

            Log.v(TAG, "Rotation: " + rotate);

            if (rotate != 0) {

                // Getting width & height of the given image.
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();

                // Setting pre rotate
                Matrix mtx = new Matrix();
                mtx.preRotate(rotate);

                // Rotating Bitmap
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);


            }

            // Convert to ARGB_8888, required by tess
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            //dialog.dismiss();

        } catch (IOException e) {
            Log.e(TAG, "Couldn't correct orientation: " + e.toString());
        }

请问可能出错?

0 个答案:

没有答案