Kesha
Kesha

Reputation: 507

Can not share image in Gmail and Instagram

I want to share Image via shareIntent. My code is given below :

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(list.get(position).getFilePath()));
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,  "Sending from myApp");//gmail-subject
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,  "Image 1234");//gmail-body
shareIntent.putExtra(Intent.EXTRA_TITLE, "Image 1234");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share via"));

I am able to send image in WhatsApp and Facebook but not in Gmail or Instagram.

When trying to share via Gmail it shows me

Can't attach file

and for Instagram it shows me

Unable to load image

.

Is anything else required to add in shareIntent ?

Upvotes: 0

Views: 530

Answers (1)

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

File file = new File(Environment.getExternalStorageDirectory() + File.separator + yourFile.getRouteFile());
                        if (file.exists()) {
                            String fileExtension = FileExtension.getFileExtension(file);
                            Log.d(TAG, "fileExtension: " + fileExtension);
                            Uri uri = Uri.parse("file://" + file.getAbsolutePath());
                            Intent share = new Intent(Intent.ACTION_SEND);
                            share.putExtra(Intent.EXTRA_STREAM, uri);
                            share.setType(fileExtension + "/*");
                            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            context.startActivity(Intent.createChooser(share, "Share file"));
                        }

get file extension

public static String getFileExtension(File file) {
        String name = file.getName();
        try {
            return name.substring(name.lastIndexOf(".") + 1);
        } catch (Exception e) {
            return "";
        }
    }

Upvotes: 1

Related Questions