10

I need to know if it is possible to share an image using only its url with a share intent. Here is my code.

Intent imageIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://eofdreams.com/data_images/dreams/face/face-03.jpg");
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(imageIntent);

So far its not working and I haven't found any helpful answers online. I would like to do this using the share intent and without downloading the image.

Amanni
  • 1,924
  • 6
  • 31
  • 51

5 Answers5

24

You can share image using share intent, but you've to decode image to a localized Bitmap

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
Uri screenshotUri = Uri.parse(path);

intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));

loadedImage is the loaded bitmap from http://eofdreams.com/data_images/dreams/face/face-03.jpg

patrick
  • 6,533
  • 7
  • 45
  • 66
Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
  • So there is no way of doing this without the bitmap? – Amanni Aug 05 '14 at 10:18
  • Can my "loadedImage" be a string to a web URL? – Martin Erlic Mar 06 '16 at 17:39
  • Works like a charm !! :D – mrnobody Jul 08 '16 at 10:28
  • any possible direct sharing? without download image? – Ranjithkumar Aug 30 '17 at 10:18
  • what about `loadedImage`? How can we load bitmap on it? – inverted_index Sep 02 '17 at 12:15
  • @inverted_index You can either use Glide or Picasso to do the heavy lifting, and get a bitmap from network or stored resource. For Glide: https://futurestud.io/tutorials/glide-callbacks-simpletarget-and-viewtarget-for-custom-view-classes For Picasso: https://stackoverflow.com/questions/20181491/use-picasso-to-get-a-callback-with-a-bitmap#answer-34390998 – Nitin Misra Sep 03 '17 at 13:29
  • Thanks for the response, I've used Fresco library, but I'm not sure how to get bitmap of the image which is loaded by Fresco. do you have any ideas? – inverted_index Sep 04 '17 at 08:35
  • exactly what i needed! – Shahid Ghafoor May 21 '18 at 11:41
  • @ShahidGhafoor How do you convert image URL to a bitmap ? I searched on it and I found that , they use a HTTP connection but it cause `networkOnMainThreadException` . – Ahmed Jul 26 '18 at 10:42
  • @Ahmed try this https://stackoverflow.com/a/8993175/4575105 or try using picasso http://square.github.io/picasso/ – Shahid Ghafoor Jul 27 '18 at 12:54
  • @ShahidGhafoor I solved it when I use `AsyncTask` to handle the `network main thread exception ` – Ahmed Jul 29 '18 at 11:37
  • @Ahmed It's way more efficient and less time consuming, if you use Picasso or Glide to get the bitmap then using an `AsyncTask` and the above said libraries gracefully handles any OOM exceptions that might occur due to `Bitmap myBitmap = BitmapFactory.decodeStream(input);` if you try to decode a large stream. – Nitin Misra Jul 30 '18 at 06:42
  • @NitinMisra How can i share any kind of file from url instead of image – KJEjava48 Jan 31 '22 at 13:24
1

See here HERE

final ImageView imgview= (ImageView)findViewById(R.id.feedImage1);

                Uri bmpUri = getLocalBitmapUri(imgview);
                if (bmpUri != null) {
                    // Construct a ShareIntent with link to image
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.setType("image/*");
                    // Launch sharing dialog for image
                    startActivity(Intent.createChooser(shareIntent, "Share Image"));    
                } else {
                    // ...sharing failed, handle error
                }

then add this to your activity :

public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
       bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
       return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(  
            Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}
0

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.

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

-2

convert url to string format

Intent imageIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://eofdreams.com/data_images/dreams/face/face-03.jpg");
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, String.valueOf(imageUri));
startActivity(imageIntent);
QArea
  • 4,955
  • 1
  • 12
  • 22
-3
Intent intent = new Intent(Intent.ACTION_SEND);

intent.setType("text/plain");

intent.putExtra(Intent.EXTRA_TEXT,"http://eofdreams.com/data_images/dreams/face/face-03.jpg"); 

startActivity(Intent.createChooser(intent, "Share Image"));                   
andrewsi
  • 10,807
  • 132
  • 35
  • 51