1

我有一个音板应用程序,并试图让用户共享我的 res/raw 文件夹中的声音。我的代码似乎正确地创建了一个文件夹并将资源复制到其中。但是,在尝试共享时,我总是从外部应用程序中收到错误消息。(例如 gmail 说:“无法附加文件”)。有人可以帮忙吗?

编码:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Pls {
    public void sendAudio(Activity activity){
        try {
            String mediaPath = copyFiletoExternalStorage(R.raw.audio_file, "audiofile.mp3",activity);
            Intent shareMedia = new Intent(Intent.ACTION_SEND);
            shareMedia.setType("audio/*");
            shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(mediaPath));
            activity.startActivity(Intent.createChooser(shareMedia, "Select app"));
        } catch (Exception e) {
            Toast.makeText(activity.getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    private String copyFiletoExternalStorage(int resourceId, String resourceName,Activity activity){
        String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
        try{
            InputStream in = activity.getResources().openRawResource(resourceId);
            FileOutputStream out = null;
            out = new FileOutputStream(pathSDCard);
            byte[] buff = new byte[1024];
            int read = 0;
            try {
                while ((read = in.read(buff)) > 0) {
                    out.write(buff, 0, read);
                }
            } finally {
                in.close();
                out.close();
            }
        } catch (IOException e) {
            Log.i("err",e.toString());
        }
        return  pathSDCard;
    }
}
4

0 回答 0