Rachid
Rachid

Reputation: 33

Android, how to Share an image file with messenger

Hello evrybody i'm tryng to share an image on messenger but i don't know why my code doesen't work, I've followed the official guide, https://developers.facebook.com/docs/messenger/android someone can told me why doese'nt work?

public void sendMessage(){
    Bitmap adv= takePic(HomeActivity.livelloCurrent.getNumeroLivello());
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        new FileOutputStream(f).write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    String mimeType = "image/jpeg";
    Intent sendIntent = new Intent();
    sendIntent.setType(mimeType);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"));
    sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
    sendIntent.setPackage("com.facebook.orca");
    try {
        startActivity(sendIntent);
    }
    catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getApplicationContext(),"Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
    }
   /** //withSDK-->// ShareToMessengerParams shareToMessengerParams = ShareToMessengerParams.newBuilder(ContentUri, mimeType).build();
    MessengerUtils.shareToMessenger(this, REQUEST_CODE_SHARE_TO_MESSENGER, shareToMessengerParams);**/
}

Im sure that the file creation work cause i have tested it . in testing I get the following error from messenger "Sorry, Messenger was not able to process the file". how can i solve ?

Upvotes: 0

Views: 1416

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

Replace:

Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg")

with:

Uri.fromFile(f)

Upvotes: 1

Related Questions