user3041073
user3041073

Reputation: 33

Share image intent - image format is not supported

I display image in my activity and want to share it through intent. I get error: Image format is not supported, regardless if it is jpg or png.

Maybe there is mistake in my code:

public class StartActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

Intent myIntent = getIntent();
int image = myIntent.getExtras().getInt("image");

ImageView imagefull = (ImageView)findViewById(R.id.imageView1);
imagefull.setImageResource(image);


Button share = (Button) findViewById (R.id.buttonshare);
share.setOnClickListener(new OnClickListener() 

{
    @Override
    public void onClick(View arg0) {

    Intent share = new Intent(Intent.ACTION_SEND);

    share.setType("image/*");

    Uri uri = Uri.fromFile(new File(getFilesDir(), "facebook.png"));
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Image!"));

    } });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Upvotes: 1

Views: 1535

Answers (3)

palffybalazs
palffybalazs

Reputation: 11

I've tried several methods, however non of them worked for me and the parts of the operations were unclear, so here is what I use for sharing image or video type content in case having the absolute path of the data. This works well with png and jpg also.

In android manifest.xml add the following lines:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
//Other codes
<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
</provider>
//Other codes
</application>

In the resource directory res, make a new folder called xml. Place a new file into it with the same name you used in the manifest.xml at the meta-data part, in this case provider_paths.xml:

android:resource="@xml/provider_paths"

Place the following in it:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_files"
        path="/storage/"/>
</paths>

In the activity you wish to use the share function, place the following code, where path is a string variable containing the absolute path of the content, and "com.example.fileprovider", the author value of Fileprovider is based on one of the line of the fresh xml file created above like this:

android:authorities="com.example.fileprovider"

File file = new File(path);
//Checking if the file exists
if(file.exists()) {
    //Creating the Uri for the file using the provider we just created
    Uri contentUri = 
    FileProvider.getUriForFile(Gallery.this,"com.example.fileprovider", file);
    //Creating the share intent
    Intent S = new Intent(Intent.ACTION_SEND);
    //Allowing different types to be shared
    S.setType("*/*");                                        
    S.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    //Providing the uri to the intent
    S.putExtra(Intent.EXTRA_STREAM, contentUri);
    //Starting the intent, can choose from apps which are listening for the 
    //content type
    startActivity(Intent.createChooser(S, "Share content using"));
}
else{                                  
    Toast.makeText(getApplicationContext(),path + " does not 
    exist",Toast.LENGTH_SHORT).show();
}

With this it is easy to share content from the device with the path of it. The authorities and resource values are crucial in manifest.xml. One can change them of course, but then make sure to modify them at all occurances.

Resources:

Android Share Intent Image Sharing not working Except WhatsApp

https://www.geeksforgeeks.org/how-to-share-image-from-url-with-intent-in-android/

https://developer.android.com/training/sharing/send

Upvotes: 0

Bharat Vasoya
Bharat Vasoya

Reputation: 361

Try this :-

shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");

//set your message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText); 
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Uri uri = Uri.fromFile(imageFileToShare);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

Upvotes: 0

AADProgramming
AADProgramming

Reputation: 6345

Try share.setType("image/png"); for PNG and share.setType("image/jpg"); for JPEG.

Upvotes: 0

Related Questions