1

I have developed a actionbar share button, however when I run the app the button does work but it crashes the application when I try to share the images with other apps. For example if I share the image with Twitter or message the application just crashes and says that "Twitter or Message has crashed.

Please the images which I am trying to share.

private final static int[] resourceIDs = new int[]{R.drawable.attraction1, R.drawable.attraction2,
            R.drawable.attraction3, R.drawable.attraction4, R.drawable.attraction5,
            R.drawable.attraction6, R.drawable.attraction7, R.drawable.attraction8,
            R.drawable.attraction9, R.drawable.attraction10, R.drawable.attraction11,
            R.drawable.attraction12, R.drawable.attraction13, R.drawable.attraction14,
            R.drawable.attraction15, R.drawable.attraction16, R.drawable.attraction17,
            R.drawable.attraction18, R.drawable.attraction19, R.drawable.attraction20};

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share, menu);
        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.menu_item_share);

        // Fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        // Return true to display menu

        return true;
    }

    //Actionbar and toolbar icons onclick methods.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.menu_item_share) {
            String text = "Look at my awesome picture";
            Uri pictureUri = Uri.parse("android.resource://\" + getPackageName()\n" +
                    "                    + \"/drawable\"");
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, text);
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            shareIntent.setType("image/*");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(shareIntent, "Share images..."));
        }

        int id1 = item.getItemId();

        if (id1 == android.R.id.home) {
            Intent intent= new Intent(this,MenuActivity.class);
            startActivity(intent);
        }


        return super.onOptionsItemSelected(item);
    }

    // Call to update the share intent
    private void setShareIntent(Intent shareIntent) {
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }

}

Share_Menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/tools"
    android:label="@string/app_name"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_item_share"
        app:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass=
            "android.widget.ShareActionProvider" />
</menu>

Please help and advise, I do not understand why the sharing application crash and my own application does not.

Maddie_J
  • 171
  • 3
  • 15
  • Please share your logcat so we can see the error output and where it comes from. – Iulian Mar 10 '16 at 17:46
  • Their is no error message as the application crashes when I try to share the image with Twitter and others. The funny thing is that my application does not crash. please advise? – Maddie_J Mar 10 '16 at 17:48

1 Answers1

0

You are trying to share images that are in your applications private directory. You cannot do this directly. You have to create a public file or create a Provider like a FileProvider.

@TCA has a good answer to this on a different post. Here is a link to his answer which contains links to two possible solutions.

https://stackoverflow.com/a/18502670/1910863

Hope this helps.

Community
  • 1
  • 1
Don Shrout
  • 865
  • 10
  • 17