2016-08-01 79 views
1

再試一次,當我的內容分享到WhatsApp的,它返回到與Toast通知共享頁「共享失敗,請再試一次」共享失敗,請(只在WhatsApp的)

我的代碼

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; 

和我的logcat

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 
+0

除了WhatsApp的,其工作像聚會,郵件,加息,文本等其它應用程序,我編譯的SDK和目標SDK是23,我在物理測試設備android one – Shubham

+0

嗨哈里,迄今爲止的解決方案? –

+0

嗨@RishabhBhatia我得到的解決方案,它爲我工作只需按照此鏈接http://stackoverflow.com/a/38697846/5753575 – Shubham

回答

0

在我身邊,它的工作細下方的Android 6.0設備。我在Android 6.0上遇到了這個問題。問題只是「外部存儲權限未被用戶授予」。 現在在啓動共享意向之前檢查外部存儲權限...

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(); 
    } 

} 
相關問題