10

我正在尝试Uri从文件夹中包含在项目中的原始文件中获取rawFileNotFoundException但无论如何,我得到了一个。

该文件是一个.wav文件,也尝试了它.mp4,也不起作用。MediaPlayer使用DOES播放这两个文件。

Uri回报:mark.dijkema.android.eindopdracht/2130968576

我的代码:

package mark.dijkema.android.eindopdracht;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PlayWaveFile();
    }

    private void PlayWaveFile()
    {
        // define the buffer size for audio track
        int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
        int bufferSize = 512;
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);

        Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.usa_for_africa_we_are_the_world);
        File file = new File(url.toString());

        int count = 0;
        byte[] data = new byte[bufferSize];

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            audioTrack.play();

            while((count = dataInputStream.read(data, 0, bufferSize)) > -1)
            {
                audioTrack.write(data, 0, count);
            }

            audioTrack.stop();
            audioTrack.release();
            dataInputStream.close();
            fileInputStream.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

错误:

java.io.FileNotFoundException: /mark.dijkema.android.eindopdracht/2130968576: open failed: ENOENT (No such file or directory)
4

5 回答 5

30

尝试这种方法,getResources().openRawResource(ResourceID)用作您的输入流。某处沿着这个:

//FileInputStream fileInputStream = new FileInputStream(file);
InputStream inputStream  = getResources().openRawResource(R.raw.usa_for_africa_we_are_the_world);
DataInputStream dataInputStream = new DataInputStream(inputStream);
audioTrack.play();

getResources().openRawResource(ResourceID)返回一个输入流

编辑:如果您使用上述方法,请删除这些代码

Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.usa_for_africa_we_are_the_world);
File file = new File(url.toString());

希望这有帮助,祝你好运!^^

于 2013-05-28T12:04:35.240 回答
6

试试这个:

uri = Uri.parse(
                ContentResolver.SCHEME_ANDROID_RESOURCE
                        + File.pathSeparator + File.separator + File.separator
                        + context.getPackageName()
                        + File.separator
                        + R.raw.myrawname
        );
于 2019-07-27T13:15:20.570 回答
3

以下是一些可能对某人有所帮助的方法:

    public Uri getRawUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/raw/" + filename);
    }
    public Uri getDrawableUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/drawable/" + filename);
    }
    public Uri getMipmapUri(String filename) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/mipmap/" + filename);
    }

只需像这样调用方法:

Uri rawUri = getRawUri("myFile.filetype");
于 2018-03-28T22:01:55.157 回答
2

您可以像这样将 InputStream 打开到原始资源:

InputStream rawInputStream = getResources().openRawResource(R.raw.usa_for_africa_we_are_the_world)
DataInputStream dataInputStream = new DataInputStream(rawInputStream);
于 2013-05-28T12:07:27.893 回答
0

不要在代码中硬编码东西!用这个:

val uri = Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .appendPath("${R.raw.your_music_file_or_whatever}")
    .build()
于 2021-06-03T22:20:12.110 回答