0

现在我正在开发android摄影应用程序,我想使用AsyncTask保存图像。首先我想使用onPreExecute()显示该iam的进度对话框,叠加图像将在doInBackground()中执行,最后我想保存我将在 onPostExecute() 上编写调用 save() 的图像。但是当单击保存按钮时,它会强制关闭错误说...

    This is my logcat file
    11-17 12:33:56.689: E/AndroidRuntime(8753): FATAL EXCEPTION: AsyncTask #1
    11-17 12:33:56.689: E/AndroidRuntime(8753): java.lang.RuntimeException: An error occured while executing doInBackground()
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.lang.Thread.run(Thread.java:1019)
    11-17 12:33:56.689: E/AndroidRuntime(8753): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.ViewRoot.checkThread(ViewRoot.java:3035)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.ViewRoot.requestLayout(ViewRoot.java:636)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.widget.ImageView.setImageDrawable(ImageView.java:322)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.widget.ImageView.setImageBitmap(ImageView.java:336)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share.apllyEffect(share.java:145)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share.access$0(share.java:122)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:106)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:1)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     ... 4 more
    11-17 12:33:56.689: E/AndroidRuntime(8753): [Blue Error Handler] Make Debugging Report file for AsyncTask #1
    11-17 12:33:56.689: E/AndroidRuntime(8753): java.lang.RuntimeException: An error occured while executing doInBackground()
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.lang.Thread.run(Thread.java:1019)
    11-17 12:33:56.689: E/AndroidRuntime(8753): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.ViewRoot.checkThread(ViewRoot.java:3035)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.ViewRoot.requestLayout(ViewRoot.java:636)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.view.View.requestLayout(View.java:8267)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.widget.ImageView.setImageDrawable(ImageView.java:322)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.widget.ImageView.setImageBitmap(ImageView.java:336)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share.apllyEffect(share.java:145)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share.access$0(share.java:122)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:106)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at com.outthinking.instatext.share$AsyncClass.doInBackground(share.java:1)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    11-17 12:33:56.689: E/AndroidRuntime(8753):     ... 4 more
    11-17 12:33:57.279: E/WindowManager(8753): [WindowManagerImpl.java:290:closeAll()] Activity com.outthinking.instatext.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40543970 that was originally added here
    11-17 12:33:57.279: E/WindowManager(8753): android.view.WindowLeaked: Activity com.outthinking.instatext.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40543970 that was originally added here
    11-17 12:33:57.279: E/WindowManager(8753):  at android.view.ViewRoot.<init>(ViewRoot.java:265)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.app.Dialog.show(Dialog.java:241)
    11-17 12:33:57.279: E/WindowManager(8753):  at com.outthinking.instatext.share$AsyncClass.onPreExecute(share.java:96)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.os.AsyncTask.execute(AsyncTask.java:391)
    11-17 12:33:57.279: E/WindowManager(8753):  at com.outthinking.instatext.share.onClick(share.java:182)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.view.View.performClick(View.java:2485)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.view.View$PerformClick.run(View.java:9080)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.os.Handler.handleCallback(Handler.java:587)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.os.Handler.dispatchMessage(Handler.java:92)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.os.Looper.loop(Looper.java:130)
    11-17 12:33:57.279: E/WindowManager(8753):  at android.app.ActivityThread.main(ActivityThread.java:3737)
    11-17 12:33:57.279: E/WindowManager(8753):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-17 12:33:57.279: E/WindowManager(8753):  at java.lang.reflect.Method.invoke(Method.java:507)
    11-17 12:33:57.279: E/WindowManager(8753):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
    11-17 12:33:57.279: E/WindowManager(8753):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
    11-17 12:33:57.279: E/WindowManager(8753):  at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

    public class AsyncClass extends AsyncTask<Void, String, Bitmap> { 
                    private Context context; 
                    ProgressDialog dialog; 

                        public AsyncClass(Context cxt) { 
                            context = cxt; 
                            dialog = new ProgressDialog(context); 
                        } 

                        @Override 
                        protected void onPreExecute() { 
                            dialog.setTitle("Please wait save the image..."); 
                            dialog.show(); 
                        } 

                        @Override 


                        protected Bitmap doInBackground(Void... unused) { 


                            //Bitmap original = getResizedBitmap(ImageViewShow.bitmap_img, sharewidth,shareheight);
                            Bitmap bmp1 = apllyEffect();
                            //if()
                            return bmp1; 
                        } 





                        @Override 
                        protected void onPostExecute(Bitmap bmp) { 
                            String fieName = UUID.randomUUID().toString();   
                            SaveImage(fieName, 100,bmp);
                            dialog.dismiss(); 
                        } 
                    } 
    and save code is...
    private Bitmap apllyEffect() {
                    if(second.frame_bitmap==null)
                    {
                      second.theam_bitmap = BitmapFactory.decodeResource(getResources(), second.current_theam);

                     second.bmOverlay = Bitmap.createBitmap(second.canvas.getWidth(), second.canvas.getHeight(), second.theam_bitmap.getConfig());
                       second.canvas = new Canvas(second.bmOverlay);

                       second.canvas.setBitmap(second.bmOverlay);
                       second.canvas.drawBitmap(second.theam_bitmap, new Matrix(), null);
                       Typeface typeface=stylesapply(second.typefacevalue);
                       share_tv.setTypeface(typeface);
                       share_tv.setTextColor(second.current_color);
                       share_img.setImageBitmap(second.bmOverlay);  
                    }else{
                   second.theam_bitmap= BitmapFactory.decodeResource(getResources(), second.current_theam);
                   second.frame_bitmap = BitmapFactory.decodeResource(getResources(), second.current_frame);
                   second.bmOverlay = Bitmap.createBitmap(second.canvas.getWidth(), second.canvas.getHeight(), second.theam_bitmap.getConfig());
                   second.canvas = new Canvas(second.bmOverlay);
                   second.canvas.setBitmap(second.bmOverlay);
                   second.canvas.drawBitmap(second.theam_bitmap, new Matrix(), null);
                   second.canvas.drawBitmap(second.frame_bitmap, new Matrix(), null);
                   share_tv.setTextColor(second.current_color);
                   share_img.setImageBitmap(second.bmOverlay);
                    }
                    return second.bmOverlay;
                }
4

4 回答 4

1

您只能从主线程调用 UI 操作。doInBackground()使用它自己的线程。处理 AsyncTask 的进度覆盖onProgressUpdate()方法。

于 2012-11-20T05:34:19.070 回答
1

看起来您正在尝试从 ASyncTask 执行 UI 操作。您应该只从 和 执行onPreExecuteUIonPostExecute操作onProgressUpdate。在 doInBackground 中,您不应直接访问 UI 元素。

检查您在第 106 行的 share.java 中的代码,该代码正在调用第 145 行的内容...

当你这样做

share_img.setImageBitmap(...)

这绝对是 UI 操作……你应该把它移到其他地方。还有所有的操作share_tv

任一种使用onProgressUpdate,如果只是在末尾doInBackground,你可以把它移到onPostExecute。否则,您也可以使用runOnUiThread

于 2012-11-20T05:33:41.193 回答
0

您不能在后台线程中执行 UI 操作。如果你这样做,你会得到一个android.view.ViewRoot$CalledFromWrongThreadException.

将您的 UI 操作移至您的onPostExecute()方法。

于 2012-11-20T05:34:50.800 回答
0

- doInBackground()方法用于在非 UI 线程上执行非 UI 工作onPost()方法用于在专用 UI​​ 线程上执行 UI 工作

-我认为您正在尝试对该方法执行 UI 工作doInBackground(),这导致它失败。

于 2012-11-20T05:33:29.747 回答