Android Share Image from Url

share image with URL android share intent

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

share image from URL to other apps

You have to build content URI from the url. There are several ways to do this.

One way is to build that is download image from url and build URI from the downloaded file.

If you are using Glide to load image from url, then it can be done in following way:

Glide.with(context).asBitmap().load(photoUrl)
.into(object: CustomTarget<Bitmap>() {

override fun onLoadCleared(placeholder: Drawable?) {
// do your stuff, you can load placeholder image here
}

override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {

val cachePath = File(context.cacheDir, "images")
cachePath.mkdirs() // don't forget to make the directory
val stream = FileOutputStream(cachePath.toString() + "/image.png") // overwrites this image every time
resource.compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.close()

val imagePath = File(context.cacheDir, "images")
val newFile = File(imagePath, "image.png")
val contentUri: Uri = FileProvider.getUriForFile(context, "${BuildConfig.APPLICATION_ID}.provider", newFile)

val intent = Intent(Intent.ACTION_SEND)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_STREAM, contentUri)
context.startActivity(Intent.createChooser(intent, "Choose..."))

}
})

Don't forget to add provider in manifest:

    <provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>

and in provider_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache" path="/" />
</paths>

How to share image from link ,i.e, without downloading the image just share using a button

first you need to load image in glide(as you are using glide ) then you can share it to anywhere but image will be saved to storage

code to load image from glide (image is being saved to storage, you can delete it later)

       Glide.with(getApplicationContext())
.load(imagelink) \\link of your image file (url)
.asBitmap().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)

.into(new SimpleTarget<Bitmap>(250, 250) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

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

Uri screenshotUri = Uri.parse(path);

intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");

startActivity(Intent.createChooser(intent, "Share image via..."));
}

@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();

super.onLoadFailed(e, errorDrawable);
}

@Override
public void onLoadStarted(Drawable placeholder) {
Toast.makeText(getApplicationContext(), "Starting", Toast.LENGTH_SHORT).show();

super.onLoadStarted(placeholder);
}
});

If you want then delete that image from storage by adding some more code

How to Share Image+Text using Share Intent in android

Try out the below code:

String fileName = "image-3116.jpg";//Name of an image
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Mail");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Launcher");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Deal"));


Related Topics



Leave a reply



Submit