Android - How to get Uri from raw file?

梦想的初衷 提交于 2019-11-28 01:02:51

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);

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:

uri = Uri.parse(
                ContentResolver.SCHEME_ANDROID_RESOURCE
                        + File.pathSeparator + File.separator + File.separator
                        + context.getPackageName()
                        + File.separator
                        + R.raw.myrawname
        );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!