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
,
Try this:
uri = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE
+ File.pathSeparator + File.separator + File.separator
+ context.getPackageName()
+ File.separator
+ R.raw.myrawname
);
Here are some methods that might help someone:
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);
}
Just call the method like this:
Uri rawUri = getRawUri("myFile.filetype");
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)
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! ^^
You can open your InputStream to the raw Resource like this:
InputStream rawInputStream = getResources().openRawResource(R.raw.usa_for_africa_we_are_the_world)
DataInputStream dataInputStream = new DataInputStream(rawInputStream);