Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android share image from url

Tags:

android

share

I want to share an image using the code:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://stacktoheap.com/images/stackoverflow.png");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sharingIntent);

I made a button that call the code above. The share intent open but I got if I click on "Share by MMS": "cannot add this picture to your message". If Facebook I got only a text area without my picture.

like image 490
Coder Php Avatar asked Apr 30 '13 13:04

Coder Php


People also ask

How do I share an image URL?

Intent sharingIntent = new Intent(Intent. ACTION_SEND); Uri imageUri = Uri. parse("http://stacktoheap.com/images/stackoverflow.png"); sharingIntent. setType("image/png"); sharingIntent.

How send multiple images from URL in android programmatically?

Code Snippet: parse("android. resource://com.code2care.example.whatsappintegrationexample/drawable/image2"); Uri uri3 = Uri. parse("android. resource://com.code2care.example.whatsappintegrationexample/drawable/image3"); ArrayList<Uri> imageUriArray = new ArrayList<Uri>(); imageUriArray.


2 Answers

An adapted version of @eclass's answer which doesn't require use of an ImageView:

Use Picasso to load the url into a Bitmap

public void shareItem(String url) {
    Picasso.with(getApplicationContext()).load(url).into(new Target() {
        @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("image/*");
            i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
            startActivity(Intent.createChooser(i, "Share Image"));
        }
        @Override public void onBitmapFailed(Drawable errorDrawable) { }
        @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
    });
}

Convert Bitmap into Uri

public Uri getLocalBitmapUri(Bitmap bmp) {
    Uri bmpUri = null;
    try {
        File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        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;
}
like image 112
Aaron Shaw Avatar answered Oct 12 '22 09:10

Aaron Shaw


I use these codes from this tutorial

        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;
}

then add your application manifest

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

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
like image 23
eclass Avatar answered Oct 12 '22 09:10

eclass