androidandroid-intentandroid-imageviewshareactionprovider

Android: Update Image Uri in Intent of ShareActionProvider


I have Two Applications, and I need to share data (Text and Image) from one to another. Everything works fine, except, if I change image after changing text (I upload photo after typing the text, or I reupload the image after typing the text) , my image does not update in second Application. I need something like TextWatcher for ImageView. Any ideas ?

Here is the important parts of my 1st Application:

Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        @Override
        public void afterTextChanged(Editable s) {
            if (mShareActionProvider != null) {
                mShareActionProvider.setShareIntent(setShareIntent());
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.layout_menu, menu);

    MenuItem share = menu.findItem(R.id.menu_item_share);

    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(share);
    mShareActionProvider.setShareIntent(setShareIntent());
    invalidateOptionsMenu();
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    setMessageIntent();
    return super.onOptionsItemSelected(item);
}

private Intent setShareIntent() {
    String share = message.getText().toString();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);

    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    if (imageUri != null) {
        shareIntent.setType("image/png");
    } else {
        shareIntent.setType("text/plain");
    }
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Here goes my sharing");
    shareIntent.putExtra(Intent.EXTRA_TEXT, share);
    return shareIntent;
}

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

                imageUri = data.getData();
                String[] path = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(imageUri, path, null, null, null);
                cursor.moveToFirst();
                int index = cursor.getColumnIndex(path[0]);
                String imgString = cursor.getString(index);
                image.setImageURI(imageUri);
                break;

    }

}

And here is my 2nd Application, where I share the data:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    Intent receiver = getIntent();
    String received_text = receiver.getStringExtra(receiver.EXTRA_TEXT);

    Uri imageUri = (Uri) receiver.getParcelableExtra(Intent.EXTRA_STREAM);
    if (imageUri != null) {
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        image.setImageBitmap(bitmap);

    }
    if (received_text != null) {
        text.setText(received_text);
    }
}

Solution

  • I don't know if doing so was stupid and can cause problems in further, but right now it works perfectly fine. If Anyone think this was a bad idea, tell me why, please. This is what I had done:

    1. I declared shareIntent as Global.
    2. I put imageUrl in intent also after uploading a new photo
    3. I put imageUrl in intent in afterTextChanged() function, so everytime I change my text, my new imageUri will be set too.