簡體   English   中英

分享失敗,請重試(僅限whatsapp)

[英]sharing failed, please try again (only in whatsapp)

當我將內容分享到 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

有同樣的問題 - 解決方案是定義 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();
    }

}

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

有我的方法。

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

我遇到了這個問題,並找到了對我有用的正確答案。 你只需要提供者。

“您無法通過使用 Uri uri5 = Uri.parse(myuri); 在 android 8.0 及更高版本上獲取 uri,您必須在應用程序中使用文件提供程序來獲取文件的 uri,還需要將 Intent.FLAG_GRANT_READ_URI_PERMISSION 添加到您的共享意圖,則只有第三個應用程序可以訪問該文件

對於示例 res->xml->provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

在我們的例子中創建擴展 FileProcider 的虛擬類 GenericFileProvider.java

import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}

在您的 manifest.xml 中,在 application 標記中添加這些行

<provider
            android:name=".GenericFileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
</provider>

現在您可以在應用程序中使用以下代碼獲取 Uri for File

Uri screenshotUri = FileProvider.getUriForFile(SelectedImageActivity1.this, getApplicationContext().getPackageName() + ".provider", file);

"

參考:解決方案

在我這邊,它在低於 Android 6.0 的設備上運行良好。 我在 Android 6.0 上遇到了這個問題。 問題只是“用戶未授予外部存儲權限。”現在在啟動共享意圖之前檢查外部存儲權限......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM