get File Content from Resource and raw ID - Android App

Android examples for App:Resource

Description

get File Content from Resource and raw ID

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import android.content.res.Resources;

public class Main {
    public static String getFileContent(Resources resources, int rawId)
            throws IOException {
        InputStream is = resources.openRawResource(rawId);

        InputStreamReader isr = new InputStreamReader(is,
                Charset.forName("ISO-8859-1"));

        // We guarantee that the available method returns the total
        // size of the asset... of course, this does mean that a single
        // asset can't be more than 2 gigs.
        int size = is.available();

        // Read the entire asset into a local byte buffer.
        char[] buffer = new char[size];
        isr.read(buffer);/*ww w. ja v a  2  s. c o m*/
        isr.close();
        is.close();

        // Convert the buffer into a string.
        return new String(buffer);
    }
}

Related Tutorials