Android - How to get Uri from raw file?
Asked Answered
M

6

16

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.

The file is a .wav file, also tried it with a .mp4, also doesn't work. Playing both files with MediaPlayer DOES work.

The Uri returns: mark.dijkema.android.eindopdracht/2130968576

My Code:

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

The Error:

java.io.FileNotFoundException: /mark.dijkema.android.eindopdracht/2130968576: open failed: ENOENT (No such file or directory)
Measurable answered 28/5, 2013 at 11:55 Comment(0)
P
33

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! ^^

Polypropylene answered 28/5, 2013 at 12:4 Comment(1)
Yes, it now works. Need to fix the audio though, lot of noise in it and stuff.Measurable
C
10

Dont hardcode stuff in code! Use this:

val uri = Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .appendPath("${R.raw.your_music_file_or_whatever}")
    .build()
Carabao answered 3/6, 2021 at 22:20 Comment(0)
P
9

Try this:

uri = Uri.parse(
                ContentResolver.SCHEME_ANDROID_RESOURCE
                        + File.pathSeparator + File.separator + File.separator
                        + context.getPackageName()
                        + File.separator
                        + R.raw.myrawname
        );
Pasty answered 27/7, 2019 at 13:15 Comment(1)
Does this even work? When I use this on Android's media player I get (No such file or directory).Onto
Y
3

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");
Yuen answered 28/3, 2018 at 22:1 Comment(0)
K
2

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);
Kersey answered 28/5, 2013 at 12:7 Comment(1)
how is that a URI?Ninth
U
0

Here is an updated answer for the latest Android SDK (currently 34).

To get the raw resource's URI:

val uri = Uri.Builder()
  .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
  .path(R.raw.audiofile.toString())
  .build()
Uncommercial answered 6/3, 2024 at 18:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.