Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to get Uri from raw file?

Tags:

People also ask

How do I find the URI of a file?

In general, an URI (Uniform Resource Identifier) represents a resource. The URI class of Java represents this format You can get the URI format of a file by invoking the toURI() method.

What is the URI in Android?

A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them.

Where is the raw folder on Android?

The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw.


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)