Mustafa Nasser
Mustafa Nasser

Reputation: 271

Android ShareActionProvider Image Problems

I am trying to send an image from an app that I am developing, I have the following code but am stuck as to how to create a Uri from a file that is in my /drawable folder, any ideas? (Very confused at this point)

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.second, menu);
    MenuItem shareitem = menu.findItem(R.id.menu_item_share);
    myShareActionProvider = (ShareActionProvider) shareitem.getActionProvider();
    myShareActionProvider.setShareIntent(createShareIntent());

    ActionBar actionBar = getActionBar();
    actionBar.show();
    return true;

}

public Intent createShareIntent() {
    image = ???
    Uri uri = Uri.fromFile(image);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/png");
    return shareIntent;
}

Upvotes: 2

Views: 953

Answers (2)

Mustafa Nasser
Mustafa Nasser

Reputation: 271

And to further my question, If I have the following (Number is a variable that changes depending on the button that is pressed). Why doesn't the following work?

public Intent createShareIntent() {
    Intent intent = getIntent();
    int number = intent.getIntExtra("BUTTON NUMBER", 1);
    Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw."BUTTON NUMBER");
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setType("image/*");
    return shareIntent;

}

Upvotes: 0

Michael Eilers Smith
Michael Eilers Smith

Reputation: 8608

private Intent createShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        Uri uri = Uri.parse("android.resource://your.package.name/" + R.drawable.image);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        return shareIntent;
    }

Upvotes: 1

Related Questions