android share image from url
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;}
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" />
You need to use a local file. Like this:
Uri imageUri = Uri.parse("android.resource://your.package/drawable/fileName"); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/png"); intent.putExtra(Intent.EXTRA_STREAM, imageUri); startActivity(Intent.createChooser(intent , "Share"));
If your image is on remote server download it to the device first.