【问题标题】:sharing failed, please try again (only in whatsapp)分享失败,请重试(仅限whatsapp)
【发布时间】:2016-08-01 09:17:02
【问题描述】:

当我将内容分享到 whatsapp 时,它会返回分享页面并显示 toast 通知“分享失败,请重试”

我的代码

if (url.startsWith("share://")) {
            Uri requestUrl = Uri.parse(url);
            String pContent = requestUrl.toString().split("share://")[1];
            Toast toast=Toast.makeText(getApplicationContext(),pContent, Toast.LENGTH_LONG);
            toast.setMargin(50,50);
            toast.show();
            StringBuilder sb = new StringBuilder();
            String [] parts = pContent.split("<br />");
            for (int i = 0; i < parts.length; i++) {
                String part = parts[i];
                sb.append(part);
                sb.append('\n');
            }
            Intent share = new Intent();
            share.setAction(Intent.ACTION_SEND);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            share.putExtra(android.content.Intent.EXTRA_TEXT, (Serializable) sb);
            share.setType("*/*");
            try {
            startActivity(Intent.createChooser(share, "Share On"));
            } catch (android.content.ActivityNotFoundException ex) {
                toast = Toast.makeText(getApplicationContext(), "whatsapp not installed", Toast.LENGTH_LONG);
                toast.setMargin(50,50);
                toast.show();
            }
            return true;

还有我的日志猫

08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
08-01 14:37:42.081 1472-1514/com.example.myactivity I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3

【问题讨论】:

  • whatsapp 除外,它适用于环聊、邮件、徒步旅行、文本等其他应用程序,我的编译 sdk 和目标 sdk 是 23,我正在物理设备 android one 中进行测试
  • 嗨,Harry,到目前为止有什么解决方案吗?
  • 嗨@RishabhBhatia 我得到了解决方案,它对我有用,只需点击此链接stackoverflow.com/a/38697846/5753575
  • 就我而言,我使用了错误的示例代码 intent.setType("plain/text");相反 intent.setType("text/plain"); [另见] (faq.whatsapp.com/en/android/28000012)

标签: android android-intent webview


【解决方案1】:

遇到了同样的问题 - 解决方案是定义 MIME 类型:当尝试与文本共享意图时,附加图像设置 sharingIntent.setType("*/*") 可以正常工作,但在仅共享文本时会失败,如上所述。

解决方案:如果只分享文本集sharingIntent.setType("text/plain")

public void sendShareToWhatsAppIntent() {

    //setup intent:
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);

    //setup image extra, if exists:
    Bitmap picBitmap = getMyBitmap();
    if (picBitmap != null) {
        String url = MediaStore.Images.Media.insertImage(context.getContentResolver(), picBitmap, "", "");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
        sharingIntent.setType("*/*");
    } else {
    //if no picture, just text set - this MIME
        sharingIntent.setType("text/plain");
    }

    //setup sharing message
    String message = "My Message - hey whatsapp!"

    sharingIntent.putExtra(Intent.EXTRA_TEXT, message.toString());

   //target WhatsApp:
   sharingIntent.setPackage("com.whatsapp");


    if (sharingIntent.resolveActivity(context.getPackageManager()) != null) {
        startActivity(sharingIntent);
    } else {
        Log.w(TAG, "sendShareIntent: cant resolve intent");
        Toast.makeText(context, "whatsapp not installed", Toast.LENGTH_SHORT).show();
    }

}

【讨论】:

    【解决方案2】:

    share.setType("text/plain"); 再试一次

    【讨论】:

    • 虽然这是一个答案,但它有助于解释为什么您认为这是解决问题的方法。
    • 因为我面临同样的问题,只需添加 settype("text/plain")
    【解决方案3】:

    有我的方法。

    private fun openShareDialog(iC: Context, //
                                iPath: String)
    {
        MediaScannerConnection.scanFile( //
                iC.applicationContext, //
                arrayOf(iPath), null //
        ) { _, iUri ->
            var shareIntent = Intent(Intent.ACTION_SEND).apply {
                putExtra(Intent.EXTRA_STREAM, iUri)
                type = "image/*"
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                putExtra(Intent.EXTRA_TEXT, iC.getString(R.string.screenshot_sharing_text))
            }
    
            shareIntent = Intent.createChooser(shareIntent, iC.resources.getText(R.string.send_to)) //
                    .apply {
                        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    }
    
            iC.startActivity(shareIntent)
        }
    }
    

    这适用于图像共享以及视频共享(只是不要忘记将type更改为(例如)video/*

    如果你需要使用分享作为Pending Intent(例如作为通知中的操作按钮),那么你可以使用这个类

    适用

    class ShareScreenshotService : IntentService(SHARE_VIDEO_RECORD_SERVICE)
    {
        override fun onCreate()
        {
            super.onCreate()
            startService(Intent(this, ShareScreenshotService::class.java))
        }
    
        override fun onHandleIntent(intent: Intent?)
        {
            if (intent != //
                null && intent.hasExtra(EXTRA_SHARE_VIDEO_RECORD_PATH))
            {
                val path = intent.getStringExtra(EXTRA_SHARE_VIDEO_RECORD_PATH)
                Logger.log(Log.ERROR, TAG, path!!)
    
                openShareDialog(this, path)
    
                PushNotificationManager.getInstance(this).getVideoRecordingNotificator(this).closeNotification()
            }
        }
    
        private fun openShareDialog(iC: Context, //
                                    iPath: String)
        {
            MediaScannerConnection.scanFile( //
                    iC.applicationContext, //
                    arrayOf(iPath), null //
            ) { _, iUri ->
                var shareIntent = Intent(Intent.ACTION_SEND).apply {
                    putExtra(Intent.EXTRA_STREAM, iUri)
                    type = "image/*"
                    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    putExtra(Intent.EXTRA_TEXT, iC.getString(R.string._screenshot_sharing_text))
                }
    
                shareIntent = Intent.createChooser(shareIntent, iC.resources.getText(R.string._send_to)) //
                        .apply {
                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        }
    
                iC.startActivity(shareIntent)
            }
        }
    
        companion object
        {
            private val TAG = ShareScreenshotService::class.java.simpleName
            private const val SHARE_VIDEO_RECORD_SERVICE_REQUEST_CODE = 3
    
            const val EXTRA_SHARE_VIDEO_RECORD_PATH = "_extra_share_video_record_path"
            const val SHARE_VIDEO_RECORD_SERVICE = "_share_video_record_service"
    
            @JvmStatic
            fun pendingIntent(context: Context, //
                              iPath: String): PendingIntent
            {
                val intent = Intent(context, ShareScreenshotService::class.java)
                intent.putExtra(EXTRA_SHARE_VIDEO_RECORD_PATH, iPath)
    
                return PendingIntent.getService( //
                        context, //
                        SHARE_VIDEO_RECORD_SERVICE_REQUEST_CODE, //
                        intent, //
                        PendingIntent.FLAG_UPDATE_CURRENT //
                )
            }
        }
    }
    

    用法

    NotificatonBuilder.addAction(R.drawable.ic_share, iC.getString(R.string.share), ShareScreenshotService.pendingIntent(iC, iImagePath))

    别忘了将Service 添加到Manifest file

    【讨论】:

      【解决方案4】:

      在我这边,它在 Android 6.0 以下的设备上运行良好。我在 Android 6.0 上遇到了这个问题。问题只是“用户未授予外部存储权限。” 现在在启动共享意图之前检查外部存储权限...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 2016-02-25
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多