分享失败,请再试一次(仅限whatsapp)

时间:2016-08-01 09:17:02

标签: android android-intent webview

当我向whatsapp分享内容时,它会返回分享页面,其中包含Toast通知“分享失败,请再试一次”

我的代码

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

和我的logcat

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotate), name: UIDeviceOrientationDidChangeNotification, object: nil)

4 个答案:

答案 0 :(得分:4)

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

解决方案:如果仅共享文本集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();
    }

}

答案 1 :(得分:0)

在我的身边它在Android 6.0以下设备上正常工作。我在Android 6.0上遇到了这个问题。问题只是"用户未授予外部存储权限。 " 现在在启动共享意图之前检查外部存储许可...

答案 2 :(得分:0)

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

答案 3 :(得分:0)

有我的方法。

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