Sharing image url to Other installed apps in mobile Android

Ashwini Jadhav
Oct 28, 2020

There will be lots of answer you will find in SO. Few answers will work and few will not .To download image from url we need to 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..."))

}
})

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>

make a new resource file called xml in res . In res create a new file called provider_paths: — —

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

Then you are done :) — -

--

--