0

I'm completely new to Java programming but I have to implement the following share for Facebook Messenger Expression Apps, my issue right now is that I don't know how to get an image from my drawable. How could it be done?

This code triggers on a onclick of a button. The image is on my drawable its called img.png

public void CompartirWhatsApp(View view) {
        FacebookSdk.sdkInitialize(getApplicationContext());

        String mimeType = "image/png";
        String route = "drawable://" + R.drawable.img;;
        File file = new File(route);
        Uri contentUri = Uri.fromFile(file);
        // contentUri points to the content being shared to Messenger
        ShareToMessengerParams shareToMessengerParams =
                ShareToMessengerParams.newBuilder(contentUri, mimeType)
                        .build();

        // Sharing from an Activity
        MessengerUtils.shareToMessenger(
                this,
                SHARE_TO_MESSENGER_REQUEST_CODE,
                shareToMessengerParams);
    }
David Ortega
  • 915
  • 9
  • 25

3 Answers3

0

You need to change your Uri. R.drawable.img is a resource id.

Example:

Uri uri = Uri.parse("android.resource://your.package.here/drawable/img");

Change "your.package.here" with the package-name of your application.


EDIT

I don't have experience with the FacebookSdk. However, a quick Google search shows code snippets and problems that relate to your question. For example, this code snippet call's setMetaData. Example:

ShareToMessengerParams shareToMessengerParams = ShareToMessengerParams.newBuilder(uri,"image/jpeg").setMetaData("{ \"image\" : \"img\" }").build();

Some more related links:

Facebook Messenger sending metadata (Android)

Get the URI of an image stored in drawable

Community
  • 1
  • 1
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Doesn't crash but it still can't get the image – David Ortega May 10 '16 at 22:55
  • I thought that was only necessary when sending additional metadata, I will try it out, sorry I'm still very new to Android understanding this is a bit complex since I've only worked with PHP and JS before :P – David Ortega May 10 '16 at 23:05
0

You can determined the URI by name as follows:

Uri uri = Uri.parse("android.resource://<package name>/drawable/image_name");

...Ensure that you put your package name in the line above.

You can also use the resource name instead of a string name.

NoCake
  • 84
  • 7
0

By definition you cannot share files in your resources directly, as they belong to your private app files. You can only share them if inserted into media database or copied somewhere on external storage or external cache. For most parts you will require permission to write storage for that. You can save to external files cache dir, but that would also require permission on api below kitkat. Sop basically you can retrieve bitmap object from your resources, but you will need to make it accessible with Uri that can be accessed by sharing applications. You can also try granting uri permission, PROVIDED you use direct uri and NOT uri based on file. Since if it's based on file, you have a problem with access.

daxgirl
  • 762
  • 4
  • 10
  • This explains it, but how could I get direct uri to invoke a previously created file by saving it from drawable to sdcard? So far I can't duplicate the image on external storage, even when including the permision. – David Ortega May 11 '16 at 22:09