read Resource by ID - Android App

Android examples for App:Resource

Description

read Resource by ID

Demo Code


//package com.java2s;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;

import java.io.Writer;

public class Main {
    public static String readResource(Context c, int id) throws IOException {
        InputStream is = c.getResources().openRawResource(id);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {/* w ww  . ja  va  2s.  c o m*/
            Reader reader = new BufferedReader(new InputStreamReader(is,
                    "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    }
}

Related Tutorials