Why doesn't this code attach an image to MMS message?

做~自己de王妃 提交于 2019-12-25 02:09:32

问题


The code is pretty basic

share_button.setOnClickListener(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.


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/7175498/why-doesnt-this-code-attach-an-image-to-mms-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!