0

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 activity-->

     val bundle: Bundle = getIntent().getExtras()!!

     val imgUrl: String = bundle.getString("image")!!
     val imageUri = Uri.parse(imgUrl)

1-->>>

full code:--> referred from -->https://stackoverflow.com/questions/49011212/sharing-image-using-intent-on-whats-app-getting-error-sharing-failed

 val bundle: Bundle = getIntent().getExtras()!!

                val imgUrl: String = bundle.getString("image")!!
                 val imageUri = Uri.parse(imgUrl)

                shareiamge.setOnClickListener {

                    shareImage(imageUri)
                      }
                    private fun shareImage(imagePath: Uri) {
    val sharingIntent = Intent(Intent.ACTION_SEND)
    sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
    sharingIntent.type = "image/*"
    sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath)
    //sharingIntent.setPackage("com.whatsapp"); for whatsapp only
    startActivity(
        Intent.createChooser(
            sharingIntent,
            "Share Image Using"
        )
    ) // for all generic options
}

Manifest-->

 <activity
        android:name=".ProductDetails.Product_details"
        android:launchMode="singleInstance" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" /> <!-- Send
     action required to display activity in share list -->
            <category android:name="android.intent.category.DEFAULT" /> <!--
      Make activity default to launch -->
            <!-- Mime type i.e. what can be shared with this activity only image and text -->
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
        </intent-filter>
    </activity>

above code output:-->

when sharing to whatsapp or any app the file format is not supported

2--->>>refered from this-->>> Share text OR Image On Whatsapp in Android

               shareiamge.setOnClickListener {


                    val whatsappIntent = Intent(Intent.ACTION_SEND)
                    whatsappIntent.type = "image/*"
                    whatsappIntent.putExtra(
                        Intent.EXTRA_STREAM,
                        imageUri
                    ) //add image path

                    startActivity(Intent.createChooser(whatsappIntent, "Share image using"))
               }

above code output:-->

when sharing to whatsapp or any app the file format is not supported

3-->>>

                val bundle: Bundle = getIntent().getExtras()!!

                val imgUrl: String = bundle.getString("image")!!
                val imageUri = Uri.parse(imgUrl)

                shareiamge.setOnClickListener {


                    val whatsappIntent = Intent(Intent.ACTION_SEND)
                    whatsappIntent.type = "image/*"
                    whatsappIntent.putExtra(
                        Intent.EXTRA_STREAM,
                        Uri.parse(res?.body()!!.data.product_images.get(0).image)
                    ) //direct image from retrofit response

                    startActivity(Intent.createChooser(whatsappIntent, "Share image using"))

above code output:-->

when sharing to whatsapp or any app the file format is not supported

4-->>refered from this--> https://stackoverflow.com/a/25136183/12553303

val bundle: Bundle = getIntent().getExtras()!!

                val imgUrl: String = bundle.getString("image")!!
                val imageUri = Uri.parse(imgUrl)

                shareiamge.setOnClickListener {
                    val intent = Intent(Intent.ACTION_SEND)
                    intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image")
                    val path: String =
                        MediaStore.Images.Media.getContentUri(imgUrl).toString()
                    val screenshotUri = Uri.parse(path)

                    intent.putExtra(Intent.EXTRA_STREAM, screenshotUri)
                    intent.type = "image/*"
                    startActivity(Intent.createChooser(intent, "Share image via..."))

above code output:-->

when sharing to whatsapp or any app --> sharing failed,please try again later

5--> but it is only sharing text not image

                   val sendIntent = Intent()
                    sendIntent.action = Intent.ACTION_SEND
                    sendIntent.putExtra(Intent.EXTRA_TEXT, imgUrl)
                    sendIntent.type = "text/plain"
                    startActivity(sendIntent)

values in log:-

   Log.e("imgUrl",imgUrl)
   Log.e("imageUri", imageUri.toString())

E/imgUrl: http://....../uploads/prod_img/thumb/medium/9dc6234da018916e545011fa1.jpeg
E/imageUri: http://..../uploads/prod_img/thumb/medium/9dc6234da018916e545011fa1.jpeg

i want to share an image need help thanks in advance

android dev
  • 200
  • 5
  • 18
  • "fetching image from adapter to activity" -- it appears that you may be trying to pass a `Uri` between activities. This will not work well, depending on where you get the `Uri` from. Beyond that, use a concrete MIME type (not a wildcard). – CommonsWare Aug 19 '20 at 11:08
  • i have used in manifest...is it not enough @CommonsWare and also see my third method where im loading retrofit response – android dev Aug 19 '20 at 11:15
  • Well, your `Uri` needs to be a `content` `Uri`. Retrofit is not going to give you that. Moreover, your manifest has nothing to do with this. In your `Intent`, use a concrete MIME type, not a wildcard. – CommonsWare Aug 19 '20 at 11:53
  • @CommonsWare if i used like this--> `sendIntent.putExtra(Intent.EXTRA_STREAM, imgUrl) sendIntent.type = "image/jpeg"` if i select whatsapp it came back to app with showing `sharing failed please try again later` – android dev Aug 19 '20 at 12:02
  • What is the value of `imgUrl`? – CommonsWare Aug 19 '20 at 12:03
  • @CommonsWare on debugging--> Bundle[{id=7, image=http://gfsfgdgghv/uploads/prod_img/thumb/medium/da6f8d00dd9f009a543e06312.jpeg}] im not putting an original image just an jpeg dummy link ok – android dev Aug 19 '20 at 12:05
  • That is not a `Uri`, let alone one with a `content` scheme. – CommonsWare Aug 19 '20 at 12:13
  • @CommonsWare can you see my latest edit above – android dev Aug 21 '20 at 07:57

1 Answers1

3

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>
shafayat hossain
  • 1,089
  • 1
  • 8
  • 13
  • getting crash --> Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? – android dev Aug 21 '20 at 09:16
  • 1
    Try adding `FLAG_ACTIVITY_NEW_TASK` flag to intent. – shafayat hossain Aug 21 '20 at 09:17
  • it is working when activty is loaded but i want to share image when share button is clicked see my code – android dev Aug 21 '20 at 09:22
  • also i have loaded that image in my activity by code --> Glide.with(getApplicationContext()) .load(res?.body()!!.data.product_images.get(0).image).into( imagemain!! ); – android dev Aug 21 '20 at 09:23
  • noi tried it is not working ...saying in whatsapp file format does not support – android dev Aug 21 '20 at 09:27
  • if i dont put your code on onclicklistener then on actvity load it is showing options to share and it is sharing properly on whatsapp also....how can i make this work on shareimage.setonclickliterner????? – android dev Aug 21 '20 at 09:46
  • When you put this code in `onClickListener`, it showed error? or sharing options not appeared? – shafayat hossain Aug 21 '20 at 09:58
  • no error ...sharing options apperared....but if i select one contact and click on send ...it gives toast that file format does not support...i tried on gmail also..it says on taoast couldnt attach file – android dev Aug 21 '20 at 10:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220205/discussion-between-shafayat-hossain-and-apj123). – shafayat hossain Aug 21 '20 at 10:01
  • hey it worked..thanks...i missed one thing to remove... – android dev Aug 21 '20 at 10:56
  • i have one doubt why above 5 methods are not working? can you tell? – android dev Aug 21 '20 at 11:26
  • 1
    I've describe it at first line of my answer. Issue is your URI doesn't have any content. That's why those had not worked. – shafayat hossain Aug 21 '20 at 11:28