2

I have a working application which shares images over WhatsApp. I used mime type to redirect selected image to WhatsApp. But when the attachment of image is selected from the chat/WhatsAppand after selecting my application it start again with the list of various contacts in WhatsApp.

What I want is to share image directly in the chat box using my application. Here please understand by this:

when attachment selected.

When attachment is selected

directed to apps main page

directed to apps main page

random image selected and shared to whatsapp

Random image selected and shared to whatsapp

but it sends to contacts and groups instead the chat box

But it sends to contacts and groups instead the chat box :(

Here goes my sharing code:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        ImageView image = (ImageView) findViewById(R.id.full_image_view);
        Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

        File sd = Environment.getExternalStorageDirectory();
        String fileName = "desi.png";
        File dest = new File(sd, fileName);
        try {
            FileOutputStream out;
            out = new FileOutputStream(dest);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch (item.getItemId()) {
            case R.id.item:
                Uri uri = Uri.fromFile(dest);
                Intent shareIntent = new Intent();
                shareIntent.setPackage("com.whatsapp");
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.setType("image/*");
                startActivity(Intent.createChooser(shareIntent,   getResources().getText(R.string.share)));
                return true;
Markus
  • 3,225
  • 6
  • 35
  • 47

2 Answers2

1

You need to return a result. Do it the following way (taken from android developer training and this answer):

Change your code in the switch-statement:

case R.id.item:
    Uri uri = Uri.fromFile(dest);
    if (shouldReturnResult()) {
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, uri); //or ACTION_PICK
        this.setResult(Activity.RESULT_OK, shareIntent); //set result
        this.finish(); //exit activity
    } else {
        Intent shareIntent = new Intent();
        shareIntent.setPackage("com.whatsapp");
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
    }
    return true;

and add this code-snippet:

private boolean shouldReturnResult() {
    Intent intent = getIntent();
    if (intent!=null && intent.getType()!=null) {
        return intent.getType().indexOf("image/") != -1;
    } else {
        return false;
    }
}

This way your app returns an image if you open it via whatsapp, but you can still share images if an user started the "normal way".

Note:
It would be better for the users if you return the result to whatsapp as soon as they select an image, so that they don't have to click the share-icon again.

Community
  • 1
  • 1
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • Somewhere in your activity, for example after the `onOptionsItemSelected` method. – Manuel Allenspach Jul 15 '14 at 07:23
  • it says This method must return a result of type boolean then... `shouldReturnResult` is never used locally... –  Jul 15 '14 at 07:27
  • Ups. Had 2 errors in the code. I changed the code of the code-snippet (the _else_ was missing) and the switch-statement (missing `()` near `shouldReturnResult`). – Manuel Allenspach Jul 15 '14 at 07:33
  • okay but it still showing a Warning: `The method shouldReturnResult() from the type FullImageActivity is never used locally`. –  Jul 15 '14 at 07:34
  • Please undo your changes and recopy the code. `shouldReturnResult()` should be from the type boolean and used in `if(shouldReturnResult())`. – Manuel Allenspach Jul 15 '14 at 07:39
  • not working still having the same problem, the app again ask to share via! option when initialized from whatsapp. –  Jul 15 '14 at 08:07
  • Try to change ACTION_SEND to ACTION_PICK. – Manuel Allenspach Jul 15 '14 at 08:09
  • still not working... should i use `setPakage` for specific whatsapp? –  Jul 15 '14 at 08:20
  • not working even after i implement the `setPakage` to whatsapp. –  Jul 15 '14 at 10:16
0
    Intent shareIntent = new Intent();
    shareIntent.setPackage("com.whatsapp");
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);// uri of perticular picture ( from gallary or camera pic
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.share)));
Vaishali Sutariya
  • 5,093
  • 30
  • 32