在这种情况下如何不重复自己? java for android

时间:2014-12-27 07:42:29

标签: java android

我知道这是一个丑陋的代码,但我不知道更好的方法。不能完全使用for循环,因为我们每次单击按钮时只拍摄一张照片。我怎样才能避免重复?

重复数字1:(拍照)

         @Override
         public void onClick(View view) {

             Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

             //Getting the date, minues & seconds
             Date date = new Date();
             DateFormat df = new SimpleDateFormat("MM-dd-hh-mm-ss");

             if (picOne == null) {
                 picOne = "gsiDoc-" + df.format(date);
                 File photo = new File(getExternalFilesDir(null), picOne + ".jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
             } else if (picTwo == null) {
                 picTwo = "gsiDoc-" + df.format(date);
                 File photo = new File(getExternalFilesDir(null), picTwo + ".jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
             } else if (picThree == null) {
                 picThree = "gsiDoc-" + df.format(date);
                 File photo = new File(getExternalFilesDir(null), picThree + ".jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
             } else if (picFour == null) {
                 picFour = "gsiDoc-" + df.format(date);
                 File photo = new File(getExternalFilesDir(null), picFour + ".jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
             } else if (picFive == null) {
                 picFive = "gsiDoc-" + df.format(date);
                 File photo = new File(getExternalFilesDir(null), picFive + ".jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
             } else if (picSix == null) {
                 picSix = "gsiDoc-" + df.format(date);
                 File photo = new File(getExternalFilesDir(null), picSix + ".jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                 btnCamera.setEnabled(false);
             }

             //Starting Activity
             startActivityForResult(intent, 19);
         }

重复数字2:(将每张图片附加到电子邮件中)

    private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("josh@josh.com", "Josh Thompson"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
    message.setSubject(subject);

    //Message
    MimeBodyPart mbp1 = new MimeBodyPart();
    mbp1.setText(messageBody);

    //Blending
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);

    if (picOne != null) {
        MimeBodyPart mbpc1 = new MimeBodyPart();
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + picOne + ".jpg");
        mbpc1.setDataHandler(new DataHandler(source));
        mbpc1.setFileName(picOne + ".jpg");
        mp.addBodyPart(mbpc1);
    }
    if (picTwo != null) {
        MimeBodyPart mbpc2 = new MimeBodyPart();
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + picTwo + ".jpg");
        mbpc2.setDataHandler(new DataHandler(source));
        mbpc2.setFileName(picTwo + ".jpg");
        mp.addBodyPart(mbpc2);
    }
    if (picThree != null) {
        MimeBodyPart mbpc3 = new MimeBodyPart();
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + picThree + ".jpg");
        mbpc3.setDataHandler(new DataHandler(source));
        mbpc3.setFileName(picThree + ".jpg");
        mp.addBodyPart(mbpc3);
    }
    if (picFour != null) {
        MimeBodyPart mbpc4 = new MimeBodyPart();
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + picFour + ".jpg");
        mbpc4.setDataHandler(new DataHandler(source));
        mbpc4.setFileName(picFour + ".jpg");
        mp.addBodyPart(mbpc4);
    }
    if (picFive != null) {
        MimeBodyPart mbpc5 = new MimeBodyPart();
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + picFive + ".jpg");
        mbpc5.setDataHandler(new DataHandler(source));
        mbpc5.setFileName(picFive + ".jpg");
        mp.addBodyPart(mbpc5);
    }
    if (picSix != null) {
        MimeBodyPart mbpc6 = new MimeBodyPart();
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + picSix + ".jpg");
        mbpc6.setDataHandler(new DataHandler(source));
        mbpc6.setFileName(picSix + ".jpg");
        mp.addBodyPart(mbpc6);
    }

    message.setContent(mp);
    return message;
}

如您所见,重复数字2 有点棘手。对于重复数字1 ,我们可以使用String pic[] = new String[7],每次按下按钮,我们可以在全局int变量上添加+1,就像我在下面尝试的那样。

            n = n+1;

             pic[n] = "gsiDoc-" + df.format(date);
             File photo = new File(getExternalFilesDir(null), pic[n] + ".jpg");
             intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

             if (n == 6) {
                 btnCamera.setEnabled(false);
             }

但是,我尝试将MimeBodyPart mbpc[] = new MimeBodyPart[7]与for循环一起使用,如下所示:

        for (int i = 0; i < 6; i++) {
        FileDataSource source = new FileDataSource(getExternalFilesDir(null) + "/" + pic[i] + ".jpg");
        mbpc[i].setDataHandler(new DataHandler(source));
        mbpc[i].setFileName(pic[i] + ".jpg");
        mp.addBodyPart(mbpc[i]);
        }

如果我尝试运行上面的代码,应用程序崩溃,我收到以下错误:

        12-27 05:04:26.586  17771-17771/gsi.againmail E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: gsi.againmail, PID: 17771
       java.lang.NullPointerException: Attempt to invoke virtual method 'void javax.mail.internet.MimeBodyPart.setDataHandler(javax.activation.DataHandler)' on a null object reference
        at gsi.againmail.MainActivity.createMessage(MainActivity.java:183)
        at gsi.againmail.MainActivity.sendMail(MainActivity.java:146)
        at gsi.againmail.MainActivity.access$300(MainActivity.java:39)
        at gsi.againmail.MainActivity$1.onClick(MainActivity.java:75)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

另一个问题是pic [i]即将出现:

        12-27 05:07:51.748  18591-18774/gsi.againmail W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Android/data/gsi.againmail/files/null.jpg: open failed: ENOENT (No such file or directory)
        12-27 05:07:51.748  18591-18774/gsi.againmail W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)

1 个答案:

答案 0 :(得分:1)

这是一个例子;

if (picOne == null) {
    picOne = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), picOne + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
} else if (picTwo == null) {
    picTwo = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), picTwo + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
} else if (picThree == null) {
    picThree = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), picThree + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
} else if (picFour == null) {
    picFour = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), picFour + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
} else if (picFive == null) {
    picFive = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), picFive + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
} else if (picSix == null) {
    picSix = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), picSix + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    btnCamera.setEnabled(false);
}

可以使用像loadPhoto

这样的简单方法
private String loadPhoto() {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat("MM-dd-hh-mm-ss");
    String str = "gsiDoc-" + df.format(date);
    File photo = new File(getExternalFilesDir(null), str + ".jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    return str;
}

然后

if (picOne == null) {
    picOne = loadPhoto();
} else if (picTwo == null) {
    picTwo = loadPhoto();
} else if (picThree == null) {
    picThree = loadPhoto();
} else if (picFour == null) {
    picFour = loadPhoto();
} else if (picFive == null) {
    picFive = loadPhoto();
} else if (picSix == null) {
    picSix = loadPhoto();
    btnCamera.setEnabled(false);
}

然后,如果您在阵列中拥有pic(s),则可以使用类似

的内容
String[] arr = { picOne, picTwo, picThree, picFour, picFive, picSix };
for (int i = 0; i < arr.length; i++) {
  if (arr[i] == null) {
    arr[i] = loadPhoto();
    if (i == arr.length - 1) {
      btnCamera.setEnabled(false);
    }
  }
}