0

Hi All im a newbie developing an app for android i have one application say whatsapp using which we can select the pictures from gallery and send it to our friends . likewise im creating a similar app's activity which responds to ACTION_PICK intent ..

lets say i have received the intent from whatsapp to my activity using getIntent() and im going to send the result back using setResult().. in between i would like to know how to insert the drawable image resource from my app to whats app via setResult so that whatsapp can accept the image im clicking in my app and sends it to my friends.

the below code is a reference from developer.android.com

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // Get the intent that started this activity
    Intent intent = getIntent();
    Uri data = intent.getData();

    // Figure out what to do based on the intent type
    if (intent.getType().indexOf("image/") != -1) {
        // Handle intents with image data ...

       **// wanted to know what code must be entered here.** 


    } else if (intent.getType().equals("text/plain")) {
        // Handle intents with text ...
    }
}


// Create intent to deliver some kind of result data
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri");
setResult(Activity.RESULT_OK, result);
finish();
Ezhil
  • 21
  • 3

2 Answers2

1

Passing images can be slow because sometimes images can be really big. You can save it to hard disk and pass the file path back to your activity and have your activity open it.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
1

The easiest way is what @wtsang02 suggested. There are couple of other approaches if you do not feel like touching file system.

  1. Use MemoryFile. You then can obtain a file descriptor that is easy to pass in Intent (check this and this).

  2. I think this is a cleaner approach, but you would need to benchmark it to see if it works for you. You can get byte array from your Drawable, send it as a Parcelable and then reconstruct Drawable back from byte[]:

    byte[] drawableBytes = [your byte array];
    ByteArrayInputStream is = new ByteArrayInputStream(drawableBytes);
    Drawable drw = Drawable.createFromStream(is, "drawable");
    
Community
  • 1
  • 1
Audrius
  • 2,836
  • 1
  • 26
  • 35