现在我正在开发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;
}