分享意图不在android marshmallow中工作

时间:2017-12-23 06:21:18

标签: android share-intent

我使用以下代码在Android应用程序中共享内容,代码适用于Android 7. +但不适用于marshmallow(6.0.1)。请建议。

shareNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
           Intent sharingIntent = new Intent(Intent.ACTION_SEND);
           sharingIntent.setType("text/plain");
           String shareBody = Constants.SHARE_TEXT + refer_code;
           sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Share");
           sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
           startActivity(Intent.createChooser(sharingIntent, "Share via"));
            } catch (Exception e) {
                ShowToastMsg.showToast(getActivity(), "Error Occured");
            }
        }
    });

2 个答案:

答案 0 :(得分:2)

这可能是由于运行时权限。 Android marshmallow 需要运行时权限才能访问存储在设备中的图像文件。因此,您需要在代码中赋予运行时权限。

因此请确保您提供所需的提交内容,例如 READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE ,因此请在提出请求之前小心。如果您不想处理权限部分,请执行此操作。

// Provide read access
shareIntent.setData(uriToImage);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

有一个如何使用新功能的详细指南。 Link

请访问 https://developer.android.com/training/permissions/requesting.html

注意:您也可以使用直接共享here作为教程。

答案 1 :(得分:0)

我曾经遇到过这个问题,

我添加了标记 FLAG_ACTIVITY_NEW_TASK ,并且运行正常。

希望它对你有用。

注意:我在签署apk后遇到了这个错误,所以你可能需要在签署你的apk后检查。