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,864
  • 6
  • 31
  • 51

4 Answers4

23

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
  • 5,739
  • 6
  • 42
  • 62
Nitin Misra
  • 4,392
  • 3
  • 31
  • 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;
}
-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,887
  • 1
  • 11
  • 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,995
  • 132
  • 34
  • 49