Loads a raw resource and returns the content as a string. - Android App

Android examples for App:Resource

Description

Loads a raw resource and returns the content as a string.

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
import android.content.res.Resources.NotFoundException;

public class Main {
    /**//from   w  w  w  .j a v a 2s . c  om
     * default encoding for all resources is utf-8
     */
    public static final String RESOURCE_ENCODING = "utf-8";
    /**
     * default new line character is \n
     */
    public static final String NEW_LINE = "\n";

    /**
     * Loads a raw resource and returns the content as a string.
     * 
     * @param ctx
     *            Context to load the resource from
     * @param id
     *            resource id
     * @return content as string
     * @throws NotFoundException
     *             if resource was not found
     * @throws IOException
     *             any IO exception
     */
    public static String loadStringRes(Context ctx, int id)
            throws NotFoundException, IOException {
        StringBuilder text = new StringBuilder();

        BufferedReader buffreader = new BufferedReader(
                new InputStreamReader(ctx.getResources()
                        .openRawResource(id), RESOURCE_ENCODING));

        String line;

        while ((line = buffreader.readLine()) != null) {
            text.append(line);
            text.append(NEW_LINE);
        }

        return text.toString();
    }
}

Related Tutorials