Skip to content Skip to sidebar Skip to footer

Share Image From Url To Other Apps

Dont make this as duplicate..i have tried every link and i will show following what i have tried till now i will briefly explain my code--> fetching image from adapter to activi

Solution 1:

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>() {

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

            overridefunonResourceReady(resource: Bitmap, transition: Transition<inBitmap>?) {


                val cachePath = File(context.cacheDir, "images")
                cachePath.mkdirs() // don't forget to make the directoryval 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:

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

and in provider_paths

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

Post a Comment for "Share Image From Url To Other Apps"