简体   繁体   English

与其他应用共享图像时,共享操作栏按钮崩溃

[英]share actionbar button crashes when sharing image with other apps

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. 例如,如果我与Twitter共享图像或消息,则应用程序崩溃,并说“ Twitter或Message崩溃。

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 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. 请提供帮助和建议,我不理解为什么共享应用程序崩溃而我自己的应用程序不崩溃。

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 . 您必须创建一个公共文件或创建一个类似FileProvider的提供程序。

@TCA has a good answer to this on a different post. @TCA在其他帖子上对此有很好的答案。 Here is a link to his answer which contains links to two possible solutions. 这是他的答案的链接,其中包含两个可能的解决方案的链接。

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM