开发者

Android attaching multiple files from sdcard to email

开发者 https://www.devze.com 2023-03-19 05:41 出处:网络
How to attach multiple files in email inandroid? Is there any permission required for multiple files attachment to an intent?

How to attach multiple files in email in android? Is there any permission required for multiple files attachment to an intent? I am trying with putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList uriList) method but still in doubt whether Uri class is <? extends Parcelable> or not. I am not able to attach any file to email.

This is my code ::

Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("plain/text");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"soubhabpathak2010@gmail.com"});
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Accident Capture");
sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

ArrayList<Uri> uriList = getUriListForImages();
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
Log.d(TAG, "Size of开发者_如何学C the ArrayList :: " +uriList.size());
FormHolderActivity.this.startActivity(Intent.createChooser(sendIntent, "Email:"));

and getUriListForImages() this method is defined as bellow -----


private ArrayList<Uri> getUriListForImages()  {

    ArrayList<Uri> uriList = new ArrayList<Uri>();
    String imageDirectoryPath =  Environment.getExternalStorageDirectory().getAbsolutePath()+ "/accident/";
    File imageDirectory = new File(imageDirectoryPath);
    String[] fileList = imageDirectory.list();

    if(fileList.length != 0) {
        for(int i=0; i<fileList.length; i++)
        {
            String file = "file://" + imageDirectoryPath + fileList[i];
            Log.d(TAG, "File name for Uri :: " + file);
            Uri uriFile = Uri.parse(file);
            uriList.add(uriFile);
            Log.d(TAG, "Image File for Uri :: " +(file));

        }
    }
    return uriList;
}

To, subject and body of the email is coming and I have images in the accident folder in sdcard (I am using 2.1 API level 7) but nothing is attaching even there is also no exception in logcat.Arraylist is also ok(means length OK and name of the files are ok too). Can anyone help me to solve this problem?


After 1 day work finally I am able to attach multiple image files from \sdcard\accident\ folder to email client. For attaching multiple files I had to add the images to the ContentResolver which is responsible for gallery images provider. Here is the Complete Code ---

Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("plain/text");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"soubhabpathak2010@gmail.com"});
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Accident Capture");
sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);

ArrayList<Uri> uriList = getUriListForImages();
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
Log.d(TAG, "Size of the ArrayList :: " +uriList.size());
FormHolderActivity.this.startActivity(Intent.createChooser(sendIntent, "Email:"));

So there is no change in the First Section of Code -- But Change is in getUriListForImages() method which is as follows---



    private ArrayList<Uri> getUriListForImages() throws Exception {
                ArrayList<Uri> myList = new ArrayList<Uri>();
                String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/accident/";
                File imageDirectory = new File(imageDirectoryPath);
                String[] fileList = imageDirectory.list();
                if(fileList.length != 0) {
                    for(int i=0; i<fileList.length; i++)
                    {   
                        try 
                        {   
                            ContentValues values = new ContentValues(7);
                            values.put(Images.Media.TITLE, fileList[i]);
                            values.put(Images.Media.DISPLAY_NAME, fileList[i]);
                            values.put(Images.Media.DATE_TAKEN, new Date().getTime());
                            values.put(Images.Media.MIME_TYPE, "image/jpeg");
                            values.put(Images.ImageColumns.BUCKET_ID, imageDirectoryPath.hashCode());
                            values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileList[i]);
                            values.put("_data", imageDirectoryPath + fileList[i]);
                            ContentResolver contentResolver = getApplicationContext().getContentResolver();
                            Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
                            myList.add(uri);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                return myList;
            } 

This is working fine and I am able to attach multiple image files to emulator default email client and send them successfully .


EXTRA_STREAM says this:

A content: URI holding a stream of data associated with the Intent, used with 
ACTION_SEND to supply the data being sent.
Constant Value: "android.intent.extra.STREAM"

You can not pass a set of file URIs: it will simply ignore the results (as you are observing).

EDIT: scratch that. I was wrong. This is the chunk of code in the standard Android Email client that handles multiple files.

if (Intent.ACTION_SEND_MULTIPLE.equals(mAction)
                 && intent.hasExtra(Intent.EXTRA_STREAM)) {
             ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
             if (list != null) {
                 for (Parcelable parcelable : list) {
                     Uri uri = (Uri) parcelable;
                     if (uri != null) {
                         Attachment attachment = loadAttachmentInfo(uri);
                         if (MimeUtility.mimeTypeMatches(attachment.mMimeType,
                                 Email.ACCEPTABLE_ATTACHMENT_SEND_INTENT_TYPES)) {
                             addAttachment(attachment);
                         }
                     }
                 }
             }
         }

Try doing this:

private ArrayList<Parcelable> getUriListForImages()  {
    ArrayList<Parcelable> uriList = new ArrayList<Parcelable>();

    String imageDirectoryPath =  Environment.getExternalStorageDirectory().getAbsolutePath()+ "/accident/";
    File imageDirectory = new File(imageDirectoryPath);
    String[] fileList = imageDirectory.list();

    if(fileList.length != 0) {
        for(int i=0; i<fileList.length; i++)
        {
            String file = "file://" + imageDirectoryPath + fileList[i];
            Log.d(TAG, "File name for Uri :: " + file);
            Uri uriFile = Uri.parse(file);
            uriList.add(uriFile);
            Log.d(TAG, "Image File for Uri :: " +(file));

        }
    }
    return uriList;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号