Share raw resource via WhatsApp

后端 未结 2 1106
别那么骄傲
别那么骄傲 2020-12-15 01:40
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(\"android.resource://\" + ContextID.getPackageName() + \"/\" + ResourceI         


        
相关标签:
2条回答
  • 2020-12-15 02:23
    File dest = Environment.getExternalStorageDirectory();
    InputStream in = ContextID.getResources().openRawResource(ResourceID);              
    
    try 
    {
        OutputStream out = new FileOutputStream(new File(dest, "lastshared.mp3"));  
        byte[] buf = new byte[1024];
        int len;
        while ( (len = in.read(buf, 0, buf.length)) != -1)
        {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    catch (Exception e) {}              
    
    Intent share = new Intent(Intent.ACTION_SEND);
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/lastshared.mp3"));
    share.setType("audio/*");
    ContextID.startActivity(Intent.createChooser(share, "Condividi il suono \"" + TheButton.getText() + "\""));
    return true;
    

    manifest:

    <manifest ...>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
        ...
    </manifest>
    
    0 讨论(0)
  • 2020-12-15 02:25

    please change this line from your code and you will be able to share write ("audio/mp3") as bellow instead ("audio/*")

    share.setType("audio/mp3");
    

    this is because share type for whatsapp doesn't support ("audio/*") or ("*/*")

    0 讨论(0)
提交回复
热议问题