有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在这种情况下,怎样才能不让自己重复?安卓的java

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

重复第1步:(拍照)

         @Override
         public void onClick(View view) {

             Intent intent = new Intent("安卓.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 安卓.view.View.performClick(View.java:4756)
        at 安卓.view.View$PerformClick.run(View.java:19749)
        at 安卓.os.Handler.handleCallback(Handler.java:739)
        at 安卓.os.Handler.dispatchMessage(Handler.java:95)
        at 安卓.os.Looper.loop(Looper.java:135)
        at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.安卓.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) 个答案

  1. # 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放在一个数组中,可以使用

    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);
        }
      }
    }