How to show an image through an intent being compatible with different apps

巧了我就是萌 提交于 2019-12-01 01:09:19

try

intent.setType("image/*");

for me it works for twitter, whatsapp, bluetooth....

EDIT: full code:

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");

        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
        intent.putExtra(android.content.Intent.EXTRA_TEXT, R.string.visita);
        Uri uri = Uri
        .parse("android.resource://com.package.xname/drawable/"
                + i);
        intent.putExtra(Intent.EXTRA_STREAM, uri);

I finally solved the problem storing the image at the MediaStore. Instead of using the URI of the File what I do is:

String agendaFilename = agendaFile.getAbsolutePath();

final ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, agendaFilename);
final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

And finally I use contentUriFile:

final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(android.content.Intent.EXTRA_STREAM, contentUriFile);
startActivity(Intent.createChooser(intent, "title"));

this code is more easy

Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setData(Uri.parse("mailto:"));
            intent.putExtra(intent.EXTRA_EMAIL,"XXXXX@XXXX.com");
            intent.putExtra(intent.EXTRA_SUBJECT, "XXXXX");
            intent.putExtra(Intent.EXTRA_TEXT, "XXXXX");
            intent.setType("message/rfc822");
            chosser = Intent.createChooser(intent, "Enviar Email");
            intent.putExtra(intent.EXTRA_STREAM, uri);
            startActivity(chosser);

For me using FileProvider worked out. I had it set up for taking photos with built-in camera and used it for sharing (see below).

final Uri uri = FileProvider.getUriForFile(mActivity, "com.paeuba.paragonik.fileprovider", photoFile);
Intent intent = ShareCompat.IntentBuilder.from(mActivity).setType("image/jpeg").setStream(uri).createChooserIntent();
mActivity.startActivity(intent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!