将本地文件附加到电子邮件中 [英] Attach a local file to an email

查看:193
本文介绍了将本地文件附加到电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将位于我的缓存中的图像文件附加到电子邮件中。

I can't attach an image file located in my cache to an email Intent.

我的代码:

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, DPLabelTV.getText());
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });

String image_key = mCache.getKey(Uri.parse(url));
File image_file = mCache.getFile(image_key);
Uri uri = Uri.fromFile(image_file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);                                                                                                                 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello world");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

uri的输出是:file:///data/data/com.mypackage/cache/ cf3dd860d5e9562512f699c9cccb2d16a3cdaa8f.jpg

The output of uri is: file:///data/data/com.mypackage/cache/cf3dd860d5e9562512f699c9cccb2d16a3cdaa8f.jpg

我已经尝试过这个:

emailIntent.setType("application/image");
emailIntent.setType("image/jpeg");

清单权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我得到的错误信息是:

W/Gmail   ( 4542): Error opening file to obtain size.
W/Gmail   ( 4542): java.io.FileNotFoundException: Permission denied
I/Gmail   ( 4542): Unable to get orientation of thumbnail file:///data/data/com.mypackage/cache/cf3dd860d5e9562512f699c9cccb2d16a3cdaa8f.jpg: class java.io.FileNotFoundException         /data/data/com.mypackage/cache/cf3dd860d5e9562512f699c9cccb2d16a3cdaa8f.jpg: open failed: EACCES (Permission denied)

任何人都有线索?

编辑2014年1月28日

新代码:

        // Grant permissions to all apps that can handle this intent
        List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList)
        {
            String packageName = resolveInfo.activityInfo.packageName;
            grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        startActivity(Intent.createChooser(emailIntent, "Send email..."));

清单:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mypackage"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

XML路径文件:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
     <files-path name="cache" path="cache"/>
</paths>

仍然没有工作......

and still not working...

推荐答案

其他应用无法访问您应用中的数据。一个快速的解决方案是将文件复制到外部文件夹,一旦从电子邮件活动中获得结果,删除外部文件。

Data from your app can not be accessed by other apps. A quick solution is to copy the file to the external folder and once you get the result back from the email activity, delete the external file.

String oldPath = ...;
String newPath = getExternalCacheDir() + "/" + UUID.randomUUID();

// Copy the file from old path to new path
...

Uri uri = Uri.fromFile(image_file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);                                                                                                                 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello world");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    // Delete the newPath file
}

这篇关于将本地文件附加到电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆