Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与其他应用共享图像时,共享操作栏按钮崩溃_Java_Android_Share Intent - Fatal编程技术网

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

Java 与其他应用共享图像时,共享操作栏按钮崩溃,java,android,share-intent,Java,Android,Share Intent,我开发了一个actionbar共享按钮,但是当我运行应用程序时,该按钮确实起作用,但当我尝试与其他应用程序共享图像时,它会使应用程序崩溃。例如,如果我与Twitter或message共享图像,应用程序就会崩溃,并说“Twitter或message已经崩溃 请看我想分享的图片 private final static int[] resourceIDs = new int[]{R.drawable.attraction1, R.drawable.attraction2, R

我开发了一个actionbar共享按钮,但是当我运行应用程序时,该按钮确实起作用,但当我尝试与其他应用程序共享图像时,它会使应用程序崩溃。例如,如果我与Twitter或message共享图像,应用程序就会崩溃,并说“Twitter或message已经崩溃

请看我想分享的图片

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>


请提供帮助和建议,我不理解为什么共享应用程序会崩溃,而我自己的应用程序却不会崩溃。

您正在尝试共享应用程序私有目录中的图像。您不能直接这样做。您必须创建公共文件或创建类似的提供程序

@TCA在另一篇文章中对此有一个很好的答案。这里是他的答案的链接,其中包含两个可能的解决方案的链接


希望这有帮助。

您正在尝试共享应用程序专用目录中的图像。您不能直接执行此操作。您必须创建一个公共文件或创建一个类似的提供程序

@TCA在另一篇文章中对此有一个很好的答案。这里是他的答案的链接,其中包含两个可能的解决方案的链接


希望这能有所帮助。

请共享您的日志猫,以便我们可以看到错误输出及其来源。当我尝试与Twitter和其他人共享图像时,应用程序崩溃,因此没有错误消息。有趣的是,我的应用程序没有崩溃。请提供建议?请共享您的日志猫,以便我们可以看到错误输出及其来源来自。当我尝试与Twitter和其他人共享图像时,应用程序崩溃,他们没有错误消息。有趣的是,我的应用程序没有崩溃。请建议?