Android Intent Share using an external image url

I could not help but notice that all examples of sharing images with Intent use locally stored files. Whenever I try to use an external url, facebook, twitter, etc., I can say a toast: "One or more multimedia elements cannot be added."

Do I need to save a copy of the image locally? If so, how to do it?

+6
source share
4 answers

Thanks @ user2245247 for the link. It contains the correct answer for sharing images from a remote URL.
Use an external library

// Get access to ImageView ImageView ivImage = (ImageView) findViewById(R.id.ivResult); // Fire async request to load image Picasso.with(context).load(imageUrl).into(ivImage); 

After the image has been loaded before the image, call the sharing method.

 // Can be triggered by a view event such as a button press public void onShareItem(View v) { // Get access to bitmap image from view ImageView ivImage = (ImageView) findViewById(R.id.ivResult); // Get access to the URI for the bitmap Uri bmpUri = getLocalBitmapUri(ivImage); 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 } } // Returns the URI path to the Bitmap displayed in specified ImageView 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; } 

Make sure you have the following permissions.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+17
source

Here you can share the link:

 Intent intent = new Intent(Intent.ACTION_SEND); Uri uri = Uri.parse("http://linkto.com/your_image.png"); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, String.valueOf(uri)); startActivity(intent); 
+4
source

Possible Solutions: Content Sharing

+3
source

Another alternative way to achieve this using the method from the previous answer is to

 Picasso.with(this).load(imageurl).into(shareTarget); private Target shareTarget = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { // Get access to the URI for the bitmap Uri bmpUri = getLocalBitmapUri(bitmap); if (bmpUri != null) { // Construct a ShareIntent with link to image Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text)); 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 } } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }; public Uri getLocalBitmapUri(Bitmap bmp) { // 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; } 
-1
source

Source: https://habr.com/ru/post/976567/


All Articles