2017-06-14 242 views
0

我真的不明白如何将声音分享给whatsapp。如何将.mp3文件共享到whatsapp?

我想它是这样的:

btn.setOnLongClickListener(new View.OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 



Uri uri = Uri.parse("android.resource://"+getPackageName()+"/raw/mySound"); 
      Intent share = new Intent(Intent.ACTION_SEND); 
      share.setType("audio/mp3"); 
      share.putExtra(Intent.EXTRA_STREAM, uri); 
      startActivity(Intent.createChooser(share, "Share Sound File")); 



      return true; 
     } 
    }); 

现在,如果我尝试点击了WhatsApp它说:“分享失败,请再试一次”

我在做什么错?我是新来的Android

我也试过这个代码:

 InputStream inputStream; 
     FileOutputStream fileOutputStream; 
     try { 
      inputStream = getResources().openRawResource(R.raw.testsound); 
      fileOutputStream = new FileOutputStream(
        new File(Environment.getExternalStorageDirectory(), "testsound.mp3")); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = inputStream.read(buffer)) > 0) { 
       fileOutputStream.write(buffer, 0, length); 
      } 

      inputStream.close(); 
      fileOutputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), "Sharing failed", Toast.LENGTH_LONG).show(); 

     } 

     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_STREAM, 
       Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/testsound.mp3")); 
     intent.setType("audio/mp3"); 
     startActivity(Intent.createChooser(intent, "Share sound")); 

我收到以下错误:

06-14 17:18:00.025 7072-7072/com.games.penta.testsharingsound W/System.err: java.io.FileNotFoundException: /storage/58AC-F8FD/gehirnaussetzer.mp3: open failed: EACCES (Permission denied) 
06-14 17:18:00.037 7072-7072/com.games.penta.testsharingsound W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:459) 
06-14 17:18:00.037 7072-7072/com.games.penta.testsharingsound W/System.err:  at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
06-14 17:18:00.037 7072-7072/com.games.penta.testsharingsound W/System.err:  at java.io.FileOutputStream.<init>(FileOutputStream.java:72) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at com.games.penta.testsharingsound.MainActivity$2.onLongClick(MainActivity.java:55) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.view.View.performLongClick(View.java:5306) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.widget.TextView.performLongClick(TextView.java:9478) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.view.View$CheckForLongPress.run(View.java:21271) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.os.Handler.handleCallback(Handler.java:743) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.os.Looper.loop(Looper.java:150) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5621) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 
06-14 17:18:00.038 7072-7072/com.games.penta.testsharingsound W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) 
06-14 17:18:00.039 7072-7072/com.games.penta.testsharingsound W/System.err:  at libcore.io.Posix.open(Native Method) 
06-14 17:18:00.039 7072-7072/com.games.penta.testsharingsound W/System.err:  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
06-14 17:18:00.039 7072-7072/com.games.penta.testsharingsound W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:445) 
06-14 17:18:00.039 7072-7072/com.games.penta.testsharingsound W/System.err:  ... 13 more 

我不知道这是否是正确的URI。

+0

检查此链接。也许它对你有帮助https://stackoverflow.com/questions/17996353/share-raw-resource-via-whatsapp – Dhanumjay

+0

@Dhanumjay我在我的代码中复制了接受的答案。但什么是ContentID?和ResourceID? – PentaGames

+0

@Dhanumjay但我试过另一个代码。看看我编辑的问题 – PentaGames

回答

0
Intent share = new Intent(Intent.ACTION_SEND); share.setType("audio/*"); 
share.putExtra(Intent.EXTRA_STREAM, uri); 
startActivity(Intent.createChooser(share, "Share Sound File")); 
+0

请[**添加答案**](https://stackoverflow.com/posts/44535275/edit)与您的代码解释为什么这项工作?应该用'audio/*'替换'audio/mp3'吗?这是代码转储的逻辑吗? –

相关问题