0

what should be the location of UriToimage will be given to this code, so that it can share images from the drawable directory of my app. please help with this...

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item:
                Uri uri = Uri.parse("android.resource://com.jai.desimeme/drawable/");
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.setType("image/jpeg");
                startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
     }
Mojo Jojo
  • 173
  • 2
  • 11

2 Answers2

0

You can't share images from the drawable directory via an Intent. You pass the URI of the file in the intent (not the actual file itself) and the app that receives the intent needs to be able to read from that URI. If that URI refers to a resource in the internal storage of your app, it can't do that - an app cannot read another app's data (unless it's through a Content Provider).

The simplest solution is to copy the resource to external storage first and share it from there.

EDIT:

If you search for something like "android copy drawable to sd card", you should find plenty of answers to the question of how to copy a resource, for example try How to copy the sample images from the drawable folder to the sdcard folder, programmaticaly?

In that question's answer, a single drawable resource is copied to a file referred to by the variable 'dest'. You would then get the Uri for your share intent using:

Uri uri = Uri.fromFile(dest);

Note that your share intent, as coded, is for a single item. If you want to send multiple files, use:

shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);

...where imageUris is an ArrayList of Uris.

Community
  • 1
  • 1
NigelK
  • 8,255
  • 2
  • 30
  • 28
  • how do i copy the resource to external storage p.s. i am a new to this kindly help me – Mojo Jojo Jan 21 '14 at 08:11
  • Please see my edit above. If you've any further issues, please raise a new question since this one is now answered. Thx and welcome to SO. – NigelK Jan 21 '14 at 08:32
0

Replace your line with

Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);

this instead of

Uri uri = Uri.parse("android.resource://com.jai.desimeme/drawable/");

khushbu vadi
  • 391
  • 2
  • 10