Why doesn't this code attach an image to MMS message?
The code is pretty basic
share_button.s开发者_高级运维etOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Uri image = Uri.parse("android.resource://com.mypac.app/" +
imageToSend);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, image);
startActivity(Intent.createChooser(share, "Share with"));
}
});
The variable imageToSend
is int
- ID of the image in /drawables directory.
In the share dialog, I can see the Messaging as option. I choose it but no image is attached. There is a message "an image cannot be attached". If I manually add image from the sdcard, then it's being attached to the MMS message with no problems.
What may be the problem with the code above?
EDIT
Tried the other solution: attach image from SD. This is the code.
File file = new File(Environment.getExternalStorageDirectory(),
"img.png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(share, "Share with"));
This does NOT work as well. I still get message that the file cannot be attached. And again Facebook app works flawlessly.
The intent is transfered to an external application.
The android:resource scheme is only valid locally.
That means that you have to copy the image from your resources to an external directory and link this new file in your Intent
How about using the FLAG_GRANT_READ_URI_PERMISSION
flag from the Intent class : http://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION
As the name says, this should transfer Read permission on the given Uri to the activity started by that intent.
What does your Uri.parse() return? Null maybe?
Try this:
Resources resources = context.getResources();
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
resources.getResourcePackageName(imageToSend) + '/' +
resources.getResourceTypeName(imageToSend) + '/' +
resources.getResourceEntryName(imageToSend) );
精彩评论