簡體   English   中英

在這種情況下如何不重復自己? 適用於Android的Java

[英]How to not repeat myself in this situation? java for 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]變為null:

        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)

這是一個例子。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM