그러냐

android mail send file 메일 보내기 파일 첨부 카메라 첨부파일명 지정 본문

android

android mail send file 메일 보내기 파일 첨부 카메라 첨부파일명 지정

관절분리 2017. 1. 13. 10:15
반응형


=====파일첨부=====


http://stackoverflow.com/questions/3748568/how-can-i-launch-androids-email-activity-with-an-attachment-attached-in-the-emai

http://stackoverflow.com/questions/2264622/android-multiple-email-attachment-using-intent-question

http://blog.blackmoonit.com/2010/02/filebrowser-send-receive-intents.html


public static void email(Context context, String emailTo, String emailCC,
   
String subject, String emailText, List<String> filePaths)
{
   
//need to "send multiple" to get more than one attachment
   
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent
.setType("plain/text");
    emailIntent
.putExtra(android.content.Intent.EXTRA_EMAIL,
       
new String[]{emailTo});
    emailIntent
.putExtra(android.content.Intent.EXTRA_CC,
       
new String[]{emailCC});
   
//has to be an ArrayList
   
ArrayList<Uri> uris = new ArrayList<Uri>();
   
//convert from paths to Android friendly Parcelable Uri's
   
for (String file : filePaths)
   
{
       
File fileIn = new File(file);
       
Uri u = Uri.fromFile(fileIn);
        uris
.add(u);
   
}
    emailIntent
.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context
.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent
.setType("text/html");
intent
.putExtra(Intent.EXTRA_EMAIL, new String[]{"to@example.com"});
intent
.putExtra(Intent.EXTRA_SUBJECT, "the subject");
intent
.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("the content"));
intent
.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/file.ext"));
startActivity
(intent);



출처: http://javaexpert.tistory.com/305 [나는 안드로이드다.]




=====다른방식 =====


http://lastiverse.tistory.com/67



=====카메라 사진 첨부=====


public class MainActivity extends Activity {
  private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;
    private File f;
    public File getAlbumDir()
    {

        File storageDir = new File(
                Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES
                ), 
                "BAC/"
            ); 

         // Create directories if needed
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }
return storageDir; } private File createImageFile() throws IOException { // Create an image file name String imageFileName =getAlbumDir().toString() +"/image.jpg"; File image = new File(imageFileName); return image; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.imageView = (ImageView)this.findViewById(R.id.imageView1); Button photoButton = (Button) this.findViewById(R.id.button1); photoButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { f = createImageFile(); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(cameraIntent, CAMERA_REQUEST); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); Intent i = new Intent(Intent.ACTION_SEND); i.setType("message/rfc822"); i.putExtra(Intent.EXTRA_EMAIL , new String[]{"first.last@gmail.com"}); i.putExtra(Intent.EXTRA_SUBJECT, "first picture"); i.putExtra(Intent.EXTRA_TEXT , "body of email"); Uri uri = Uri.fromFile(f); i.putExtra(Intent.EXTRA_STREAM, uri); try { startActivity(Intent.createChooser(i, "Send mail...")); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); } } } }


=====카메라 사진 첨부 여러개=====


public static void email(Context context, String emailTo, String emailCC,
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
        new String[]{emailCC});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths)
    {
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

포인트는
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

ArrayListExtra 로 보내기


반응형