Android - How to get Uri from raw file?

前端 未结 4 813
我寻月下人不归
我寻月下人不归 2020-12-03 11:58

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,

相关标签:
4条回答
  • 2020-12-03 12:47

    Try this:

    uri = Uri.parse(
                    ContentResolver.SCHEME_ANDROID_RESOURCE
                            + File.pathSeparator + File.separator + File.separator
                            + context.getPackageName()
                            + File.separator
                            + R.raw.myrawname
            );
    
    0 讨论(0)
  • 2020-12-03 12:55

    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");
    
    0 讨论(0)
  • 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! ^^

    0 讨论(0)
  • 2020-12-03 12:56

    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);
    
    0 讨论(0)
提交回复
热议问题