I can't attach an image file located in my cache to an email Intent.
My code :
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..."));
The output of uri is: file:///data/data/com.mypackage/cache/cf3dd860d5e9562512f699c9cccb2d16a3cdaa8f.jpg
I already tried this :
emailIntent.setType("application/image");
emailIntent.setType("image/jpeg");
Manifest permissions :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The error message I get is :
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)
Anyone has a clue?
EDIT FROM 28/01/14
New code :
// 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..."));
Manifest :
<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 path file :
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="cache" path="cache"/>
</paths>
and still not working...