1

I implemented a share button(code is written in Kotlin) which already can send text to whatsapp / email etc.

Now I also want to send an image, but after trying several tutorials, it still displays the same error:

Instead of the picture "image27.png", only an undefined broken file "2131099738" is send.

Here is my code:

package com.example.myapp
...
share_button.setOnClickListener {
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "image/png"

        val shareImageURI = Uri.parse("android.resource://com.example.myapp/drawable/"+R.drawable.image27)

        val shareSub = "This is for you"
        val shareBody = message 
        shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageURI)
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareSub)
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareBody)
        startActivity(Intent.createChooser(shareIntent, "Share this"))
    }

The picture is saved here: C:\Users\Bine\AndroidStudioProjects\MyApp\app\src\main\res\drawable\image27.png

How can I fix this?

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
user21681
  • 95
  • 1
  • 4
  • 1
    did you try `android.resource://com.example.myapp/drawable/image27` directly – Jyoti JK Mar 21 '18 at 08:47
  • Yes. Then there is no attachment at all. And when I write it without the file extension '.png' a file with correct name is attached, but you are not able to open it – user21681 Mar 21 '18 at 12:41

1 Answers1

0

That behaviour is intended. What are you trying to do right now is to share a drawable from you app, but I don't think this will work since every Android app runs in a Sanbox.

The Android Application Sandbox, which isolates your app data and code execution from other apps.

However, to better understand your issue, you have to learn about the R class. In a few words, it is a class generated by the system that holds a reference to everything that is found in the resources directory, including the drawables you are using. The important part here is that every reference to a resource will be saved as an int variable, hence your 2131099738 appears there when you try to use R.drawable.image27.png (Note that if you make any changes to resources, there is a huge chance that this number will change).

To solve the problem, you have two approaches:

  1. store the file in the external storage;
  2. use a ContentProvider to access the file.
Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
  • ok great. I understand. I already found `ContentResolver.openInputStream(shareImageURI)` but it does not work like that in Kotlin. Do you know how to use this? – user21681 Mar 21 '18 at 12:32
  • Well, as I already said, you can't share a drawable because of the Sandbox (it is only visible to you app). You can have a look here to see a few ways on how you can share it (https://www.youtube.com/watch?v=h9TunRURWNU), but you have to store the file in the external storage. An alternative would ve something like this https://stackoverflow.com/a/36212192/3734172, but this approach is more of a "hack" than an actual solution. The proper way to do it is to save it on external storage and then use the path to share it. – Iulian Popescu Mar 22 '18 at 07:43