Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android将多个文件从SD卡附加到电子邮件_Android_Email_Attachment - Fatal编程技术网

Android将多个文件从SD卡附加到电子邮件

Android将多个文件从SD卡附加到电子邮件,android,email,attachment,Android,Email,Attachment,如何在android中在电子邮件中附加多个文件? 将多个文件附加到一个意图是否需要任何权限? 我正在尝试使用putParcelableArrayListExtra(Intent.EXTRA_STREAM,ArrayList uriList)方法,但仍然怀疑Uri类是否为EXTRA_STREAM: A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply

如何在android中在电子邮件中附加多个文件? 将多个文件附加到一个意图是否需要任何权限?
我正在尝试使用putParcelableArrayListExtra(Intent.EXTRA_STREAM,ArrayList uriList)方法,但仍然怀疑Uri类是否为EXTRA_STREAM:

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"
您不能传递一组
文件
uri:它只会忽略结果(正如您所观察到的)

编辑:划掉那个。我错了。这是标准Android电子邮件客户端中处理多个文件的代码块

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);
                         }
                     }
                 }
             }
         }
if(Intent.ACTION\u SEND\u MULTIPLE.equals(mAction)
&&intent.hasExtra(intent.EXTRA_流)){
ArrayList=intent.getParcelableArrayListExtra(intent.EXTRA\u流);
如果(列表!=null){
用于(可包裹:列表){
Uri=(Uri)可包裹;
if(uri!=null){
附件附件=loadAttachmentInfo(uri);
如果(mime实用程序.mimetype)匹配(附件.mMimeType,
电子邮件。可接受\附件\发送\意向\类型){
附件(附件);
}
}
}
}
}
尝试这样做:

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;
}
private ArrayList getUriListForImages(){
ArrayList uriList=新的ArrayList();
字符串imageDirectoryPath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/contract/”;
File imageDirectory=新文件(imageDirectoryPath);
String[]fileList=imageDirectory.list();
如果(fileList.length!=0){

对于(int i=0;i经过一天的工作,我终于能够将\sdcard\contract\文件夹中的多个图像文件附加到电子邮件客户端。对于附加多个文件,我必须将图像添加到负责gallery images provider的ContentResolver。 这是完整的代码---

Intent sendIntent=新意图(Intent.ACTION\u SEND\u MULTIPLE);
sendIntent.setType(“纯/文本”);
sendIntent.putExtra(Intent.EXTRA_电子邮件,新字符串[]{”soubhabpathak2010@gmail.com"});
sendIntent.putExtra(Intent.EXTRA_主题,“事故捕捉”);
sendIntent.putExtra(Intent.EXTRA_TEXT,emailBody);
ArrayList uriList=getUriListForImages();
sendIntent.putParcelableArrayListExtra(Intent.EXTRA\u流,uriList);
Log.d(标记“ArrayList的大小::”+uriList.Size());
FormHolderActivity.this.startActivity(Intent.createChooser(sendIntent,“Email:”);
因此,在代码的第一部分中没有变化——但在getUriListForImages()方法中有变化,如下所示---


私有ArrayList getUriListForImages()引发异常{
ArrayList myList=新的ArrayList();
字符串imageDirectoryPath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/contract/”;
File imageDirectory=新文件(imageDirectoryPath);
String[]fileList=imageDirectory.list();
如果(fileList.length!=0){

对于(int i=0;i)如何附加多个文件?是否有其他方法?
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);
                         }
                     }
                 }
             }
         }
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;
}
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:"));


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