Read from Raw Folder in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Read from Raw Folder in Android

Postby vivekkalady » Fri Nov 21, 2014 2:47 pm

The below code blocks allows you to read different kinds of file from raw folder of an android application

Text file

Code: Select all

public static void readRaw(Context ctx,int res_id) {

    InputStream is = ctx.getResources().openRawResource(res_id);
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                        // size

    try {
        String test;
        while (true) {
            test = br.readLine();
            // readLine() returns null if no more lines in the file
            if (test == null)
                break;
        }
        isr.close();
        is.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}



Sound file

Code: Select all

MediaPlayer mediaPlayer = new MediaPlayer();
Uri uri=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test);
mediaPlayer.setDataSource(this,uri);



Video file

Code: Select all

mVideoView = (VideoView) findViewById(R.id.Video_FrontPage);
uri=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.welcom_video);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron