运行多个AsyncTask退出应用程序而不显示任何特定错误

时间:2013-08-26 09:41:54

标签: android android-asynctask android-camera javamail

我的应用程序中有一个非常简单的任务。我正在尝试在我的应用程序中实现以下功能。

1.按下Camera hardware按钮时使用自定义volume类捕获图像。一旦捕获开始,它应该会定期自动捕获图像(比方说2秒)

2.每张图片必须保存在SD卡中,并邮寄到后台的相应邮件ID。

为了实现上述目的,我创建了一个自定义Camera Classoverride硬件volume按钮来捕获图片。并且在后台发送邮件我使用了Java Mail API。< / p>

由于我必须每隔2秒发送一次邮件,我使用AsyncTask编写了邮件和保存到SD卡的代码。我从here和其他一些帖子中发现,AsyncTask的池大小是最大128 + 10 = 138总共。当我以2秒的间隔发送邮件时,它超过了最大池导致错误的大小。

但我的要求是以2秒的间隔捕获高分辨率图像并将其邮寄到相应的邮件ID。

是否可以使用AsyncTask?或任何其他框架android提供执行长时间运行的任务?

以下是我到目前为止所尝试的内容,

硬件音量键覆盖

 public boolean onKeyDown(int keyCode, KeyEvent event) 
        { 
           if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) { 

                timer = new Timer();
                TimerTask updateProfile = new CustomTimerTask(DVCameraActivity.this);
                timer.scheduleAtFixedRate(updateProfile, 0, 2000);
                Toast toast= Toast.makeText(getApplicationContext(), "Picture Capturing Started..", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.show();
           return true;
       } else {
           return super.onKeyDown(keyCode, event); 
       }
    }

CustomTimerTask

public class CustomTimerTask extends TimerTask {
        public CustomTimerTask(Context con) {
        }
        @Override
        public void run() {
             if (isCamera == true)
                mCamera.takePicture(null, null, mPicture);
        }

    }

PictureCallback

PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            new SavePhotoTask().execute(data);
             if (mCamera!= null)
            camera.startPreview();
        }

    };

SavePhotoTask()

class SavePhotoTask extends AsyncTask<byte[], String, String> {
        @SuppressLint("SimpleDateFormat")
        @Override
        protected String doInBackground(byte[]... jpeg) {


            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "Secret Camera");

            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d("MyCameraApp", "failed to create directory");
                    return null;
                }
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());
            File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + "." + mSuffix);
            try {
                FileOutputStream fos = new FileOutputStream(mediaFile);
                fos.write(jpeg[0]);
                fos.close();

                path=mediaFile.getAbsolutePath();
                SendMail(path);
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }

            return mediaFile.toString();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);


        }


    }

的SendMail()

private void SendMail(String Imagepath) {
        Mail m = new Mail(Constants.email, Constants.psw);
        String[] toArr = { Constants.email };
        m.setTo(toArr);
        m.setFrom("<EMAIL>");
        m.setSubject("Spy Camera Images");
        m.setBody("If you are watching the image, then my Spy Camera App is working..Voila!!");
        try {
            m.addAttachment(Imagepath);
            if (m.send()) {
                Log.i("MAIL SEND", "SUCCESSS");
                Send=true;
            } else {
                Send=false;
                Log.i("MAIL SEND", "FAILED");
            }
        } catch (Exception e) {
            Log.e("MailApp", "Could not send email", e);
        }
    }

Logcat错误

08-23 18:37:55.609: I/dalvikvm(12540): DALVIK THREADS:
08-23 18:37:55.609: I/dalvikvm(12540): "main" prio=5 tid=1 WAIT
08-23 18:37:55.609: I/dalvikvm(12540):   | group="main" sCount=1 dsCount=0 s=N obj=0x401b8968 self=0xcd38
08-23 18:37:55.609: I/dalvikvm(12540):   | sysTid=12540 nice=0 sched=0/0 cgrp=default handle=-1345017816
08-23 18:37:55.609: I/dalvikvm(12540):   at java.lang.Object.wait(Native Method)
08-23 18:37:55.609: I/dalvikvm(12540):   - waiting on <0x48718010> (a android.os.MessageQueue)
08-23 18:37:55.609: I/dalvikvm(12540):   at java.lang.Object.wait(Object.java:288)
08-23 18:37:55.617: I/dalvikvm(12540):   at android.os.MessageQueue.next(MessageQueue.java:146)
08-23 18:37:55.617: I/dalvikvm(12540):   at android.os.Looper.loop(Looper.java:110)
08-23 18:37:55.624: I/dalvikvm(12540):   at android.app.ActivityThread.main(ActivityThread.java:4632)
08-23 18:37:55.624: I/dalvikvm(12540):   at java.lang.reflect.Method.invokeNative(Native Method)
08-23 18:37:55.624: I/dalvikvm(12540):   at java.lang.reflect.Method.invoke(Method.java:521)
08-23 18:37:55.624: I/dalvikvm(12540):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
08-23 18:37:55.624: I/dalvikvm(12540):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
08-23 18:37:55.624: I/dalvikvm(12540):   at dalvik.system.NativeStart.main(Native Method)
08-23 18:37:55.624: I/dalvikvm(12540): "AsyncTask #34" prio=5 tid=42 MONITOR

很抱歉这么冗长的解释。我找不到更简短的方法来解释我的问题。任何建议将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

创建自己的请求队列,将每个“发送图片”请求放入队列,并使用一个邮件任务处理来自队列的请求。使用单个连接从队列发送所有请求,直到队列为空,然后关闭连接并等待下一个请求。