我如何与whatsapp共享一个mp3文件? (Android Studio)

时间:2017-06-22 15:04:00

标签: java android mp3 share whatsapp

我想与whatsapp共享一个mp3文件。 我在Stack Overflow上找到this question,但接受的答案对我不起作用。如果我尝试与whatsapp分享,则说“分享失败,请再试一次”:

button.setVisible(true);

以下是完整的MainActivity.java:

                    File dest = Environment.getExternalStorageDirectory();
                InputStream in = getResources().openRawResource(R.raw.sound);

                try
                {
                    OutputStream out = new FileOutputStream(new File(dest, "sound.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) {
e.printStackTrace();}

                Intent share = new Intent(Intent.ACTION_SEND);
                share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/sound.mp3"));
                share.setType("audio/mp3");
                startActivity(Intent.createChooser(share, "Shared"));

当然我在我的Manifest中写了这一行:

    package com.example.aaron.sharetest;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

    Button shareBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final File FILES_PATH = new File(Environment.getExternalStorageDirectory(), "Android/data/com.example.aaron.sharetest/files");

        File sharefile= new File(FILES_PATH, "sound.mp3") ;
        putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sharefile));


        shareBtn = (Button)findViewById(R.id.shareBtn);

        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1);

        shareBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {




                if (Environment.MEDIA_MOUNTED.equals(

                        Environment.getExternalStorageState())) {

                    if (!FILES_PATH.mkdirs()) {

                        Log.w("error", "Could not create " + FILES_PATH);

                    }

                } else {

                    Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();

                    finish();

                }


                File dest = Environment.getExternalStorageDirectory();
                InputStream in = getResources().openRawResource(R.raw.bibikurz);

                try
                {
                    OutputStream out = new FileOutputStream(new File(dest, "sound.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) {
                    e.printStackTrace();
                }

                Intent share = new Intent(Intent.ACTION_SEND);
                share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/sound.mp3"));
                share.setType("audio/mp3");
                startActivity(Intent.createChooser(share, "Shared"));



        }



        });
    }


}

如何将mp3文件分享给whatsapp等?

我尝试了很多接受的答案,但没有人为我工作。

这就是我在LogCat中获得的内容:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2 个答案:

答案 0 :(得分:1)

Android 6.0 Marshmallow(API 23)或更高版本。如果是这种情况,那么你可以实现runtime permissions

使用文件路径并检查存储卡

文件FILES_PATH =新文件(

Environment.getExternalStorageDirectory(),

“Android / data / com.examples(您的包裹)/文件”);

文件共享文件=新文件(

FILES_PATH,

“demo.mp3”);

putExtra(Intent.EXTRA_STREAM,Uri.fromFile(sharefile))     

@Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        if (Environment.MEDIA_MOUNTED.equals(

            Environment.getExternalStorageState())) {

            if (!FILES_PATH.mkdirs()) {

                Log.w(TAG, "Could not create " + FILES_PATH);

            }

        } else {

            Toast.makeText(this, R.string.need_external_storage, Toast.LENGTH_LONG).show();

            finish();

        }

答案 1 :(得分:0)

  

我的“sound.mp3”文件位于原始文件夹中。

您的前两个示例将无效,因为它们与raw资源无关。它们从可能也不存在的目录共享不存在的文件。

  

但在这里我不知道“ContextID”或“ResouceID”是什么。

如果此代码位于前两个代码段的位置,ContextID可以替换为this。如果您要分享res/raw/sound.mp3,则ResourceIDR.raw.sound