Android - 如何从原始文件获取Uri? [英] Android - How to get Uri from raw file?
问题描述
我想从 raw
文件夹中包含在项目中的原始文件中获取 Uri
。
但无论如何我都会得到 FileNotFoundException
。
I am trying to get the Uri
from a raw file I have included in the project in the raw
folder.
But I am getting a FileNotFoundException
, no matter what.
该文件是 .wav
文件,也尝试使用 .mp4
,也不起作用。
使用 MediaPlayer播放这两个文件
是否正常工作。
The file is a .wav
file, also tried it with a .mp4
, also doesn't work.
Playing both files with MediaPlayer
DOES work.
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)
推荐答案
试试这种方法,使用 getResources ()。openRawResource(ResourceID)
作为您的inputStream。
某处:
Try this approach, use getResources().openRawResource(ResourceID)
as your inputStream.
Somewhere along this :
//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)
返回一个InputStream
getResources().openRawResource(ResourceID)
returns an InputStream
编辑:如果你使用上述方法删除这些代码
EDIT : Remove these code if you use the above approach
Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.usa_for_africa_we_are_the_world);
File file = new File(url.toString());
希望这会有所帮助,祝你好运! ^^
Hope this helps, Good Luck! ^^
这篇关于Android - 如何从原始文件获取Uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!