使用Asset文件夹中的附件发送电子邮件

时间:2015-05-15 06:17:34

标签: android email email-attachments

    //EMAIL SENDING CODE  FROM ASSET FOLDER
    email = editTextEmail.getText().toString();
    subject = editTextSubject.getText().toString();
    message = editTextMessage.getText().toString();
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("file/html");
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.example.deepa.xmlparsing/file:///android_assets/Combination-1.html"));
    startActivity(Intent.createChooser(emailIntent, "Send email using"));

最后,我从资产文件夹(Combination-1.html)获取文件。

它正在

  

未找到运行时错误文件例外。

还有其他方式发送文件附件吗?

4 个答案:

答案 0 :(得分:7)

发送电子邮件的最简单方法是创建ACTION_SEND类型的Intent:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
intent.putExtra(Intent.EXTRA_TEXT, "Attachment test");

要附加单个文件,请向Intent添加一些扩展数据:

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/path/to/file")));
intent.setType("text/html");

或使用Html.fromHtml()构建html内容:

intent.putExtra( Intent.EXTRA_TEXT,
                 Html.fromHtml(new StringBuilder()
                 .append("<h1>Heading 1</h1>")
                 .append("<p><a>http://www.google.com</a></p>")
                 .toString()));

对于多个附件:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple");
intent.putExtra(Intent.EXTRA_TEXT, "multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

完成调用startActivity()传递intent。

答案 1 :(得分:3)

创建资产文件夹文件的File对象,并将此对象附加到您的电子邮件Intent。

正如您的问题 运行时错误文件未找到异常 中所述,这可能会导致URL “file:/// android_asset /” 不指向特定目录,它仅由WebView用于处理资产。拉了from

您可以将其作为输入流打开并将此File转换为in = new BufferedReader(new InputStreamReader(activity.getAssets().open(myfile.pdf)));

Intent intent = new Intent(Intent.ACTION_SEND ,Uri.parse("mailto:")); 
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
activity.startActivity(intent);

在电子邮件中发送此文件对象,如下所示。

Intent.ACTION_SEND

Intent.EXTRA_STREAM曾用于发送电子邮件,Intent.EXTRA_STREAM用于附件和电子邮件。您可以在单个意图中使用多个intent.setAction(Intent.ACTION_SEND_MULTIPLE);来使用intent.setType(String mimeType)引用多个附件。

image/jpeg audio/mpeg4-generic text/html audio/mpeg audio/aac audio/wav audio/ogg audio/midi audio/x-ms-wma video/mp4 video/x-msvideo video/x-ms-wmv image/png image/jpeg image/gif .xml ->text/xml .txt -> text/plain .cfg -> text/plain .csv -> text/plain .conf -> text/plain .rc -> text/plain .htm -> text/html .html -> text/html .pdf -> application/pdf .apk -> application/vnd.android.package-archive 输入参数表示您希望从触发意图(此处为intent实例)获得的MIME类型数据.where setype可以是

public class UserListAdapter extends Adapter<UserListAdapter.DataObjectHolder> implements Filterable {
private static MyClickListener myClickListener;
public Activity activity;
Bitmap bitmap;
ImageView imgUserBig;
TextView txtUserName;
RelativeLayout userProfileView;
private ArrayList<DataObject> mArrayList;
private ArrayList<DataObject> mFilteredList;    

public UserListAdapter(Activity a, ArrayList<DataObject> d, ImageView mImgUserBig, RelativeLayout viewLinear, TextView txtUsername) {

    this.activity = a;
    this.mArrayList = d;
    this.mFilteredList = d;
    this.imgUserBig = mImgUserBig;
    this.userProfileView = viewLinear;
    this.txtUserName = txtUsername;
}

public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new DataObjectHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_user_list, parent, false));
}

public void onBindViewHolder(final DataObjectHolder holder, final int position) {
    holder.txtName.setText((mFilteredList.get(position)).getmName());
    holder.txtUserName.setText((mFilteredList.get(position)).getmUsername());
    Glide.with(activity.getApplicationContext()).load((mFilteredList.get(position)).getmImage()).into(holder.imgUser);
    Glide.with(activity).load((mFilteredList.get(position)).getmImage()).asBitmap().into(new SimpleTarget<Bitmap>(Integer.MIN_VALUE, Integer.MIN_VALUE) {
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
            bitmap = resource;
            (mFilteredList.get(position)).setBitmap(bitmap);
        }
    });
    holder.imgUser.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if ((mFilteredList.get(position)).getBitmap() != null && imgUserBig != null) {
                zoomImageFromThumb(holder.imgUser, (mFilteredList.get(position)).getBitmap(), (mFilteredList.get(position)).getmImage(), (mFilteredList.get(position)).getmUsername());
            }
        }
    });
}

public int getItemCount() {
    return mFilteredList.size();
}

public void setOnItemClickListener(MyClickListener myClickListener) {
    UserListAdapter.myClickListener = myClickListener;
}

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString();
            if (charString.isEmpty()) {
                mFilteredList = mArrayList;
            }else {
                ArrayList<DataObject> filteredList = new ArrayList<>();
                for (DataObject userData : mArrayList) {
                    if (userData.getmUsername().toLowerCase().contains(charString) || userData.getmName().toLowerCase().contains(charString)) {
                        filteredList.add(userData);
                    }
                }
                mFilteredList = filteredList;
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = mFilteredList;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mFilteredList = (ArrayList<DataObject>) results.values;
            notifyDataSetChanged();
        }
    };
}

public interface MyClickListener {
    void onItemClick(int i, View view);
}

public Object getItem(int position) {
    return mFilteredList.get(position);
}



public class DataObjectHolder extends ViewHolder implements OnClickListener {
    ImageView imgUser;
    TextView txtName;
    TextView txtUserName;

    public DataObjectHolder(View itemView) {
        super(itemView);
        txtName = (TextView) itemView.findViewById(R.id.txtname);
        txtUserName = (TextView) itemView.findViewById(R.id.txtusername);
        imgUser = (ImageView) itemView.findViewById(R.id.img_user);
        itemView.setOnClickListener(this);
    }

    public void onClick(View v) {
        UserListAdapter.myClickListener.onItemClick(getPosition(), v);
    }
}}

答案 2 :(得分:0)

  

您可以使用以下代码来实现目标,并可以根据您的要求更改硬编码字符串。

String filename="contacts_sid.vcf"; 
File filelocation = new 
File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// Here you can set the type to 'email'
emailIntent .setType("abc.xyz.cursor.dir/email");
String to[] = {"android@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
//  For the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Find Attachments with email");
startActivity(Intent.createChooser(emailIntent , "Sending  email..."));

答案 3 :(得分:-1)

如果没有ContentProvider ,您无法将资源文件夹文件共享给其他应用程序。你可以发送每个应用程序都可以访问的文件。比如在手机内存或sd卡中的一些whare。

分享资产Check This Answer of @CommonsWare

或者 阅读您的文本文件并使用@RonTLV答案和邮件。

或者 在内存中复制文件并添加到发送启动。 希望这个答案会有所帮助。