get Resource String - Android App

Android examples for App:Resource

Description

get Resource String

Demo Code

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

public class Main{
    public static String getResourceString(String url)
            throws Exception {
        return new String(getResourceByteArray(url));
    }//from w ww  .  j a  va 2 s .c om
    public static byte[] getResourceByteArray(String url)
            throws Exception {
        try {
            byte[] bs = getByteArray(new URI(url).toURL().openStream());
            return bs;
        } catch (URISyntaxException e) {
            throw new Exception(e);
        } catch (IOException e) {
            throw new Exception(e);
        }
    }
    public static byte[] getByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toByteArray();
    }
}

Related Tutorials