Example usage for android.content.res Resources openRawResource

List of usage examples for android.content.res Resources openRawResource

Introduction

In this page you can find the example usage for android.content.res Resources openRawResource.

Prototype

@NonNull
public InputStream openRawResource(@RawRes int id) throws NotFoundException 

Source Link

Document

Open a data stream for reading a raw resource.

Usage

From source file:Main.java

public static String getStringFromResRaw(int iResId, Resources resources) {
    InputStream inputStream = resources.openRawResource(iResId);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    int iLen = -1;
    byte[] buffer = new byte[512];
    try {/*from   ww  w.j a  v a  2 s . c om*/
        while (-1 != (iLen = inputStream.read(buffer)))
            outputStream.write(buffer, 0, iLen);
        inputStream.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

static String readSharedFromRawResource(Resources resources, int resourceId) {
    final InputStream inputStream = resources.openRawResource(resourceId);
    final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    StringBuilder body = new StringBuilder();
    try {/*from   w  w w. j a v a  2  s .  c  o m*/
        String nextLine;
        while ((nextLine = bufferedReader.readLine()) != null) {
            body.append(nextLine);
            body.append("\n");
        }
    } catch (IOException e) {
        return null;
    }
    return body.toString();
}

From source file:Main.java

public static String readTextFileFromRawResource(final Resources res, final int resourceId) {
    final InputStream inputStream = res.openRawResource(resourceId);
    final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    String nextLine;/*from  w w w .ja  va  2s  . c  o m*/
    final StringBuilder body = new StringBuilder();

    try {
        while ((nextLine = bufferedReader.readLine()) != null) {
            body.append(nextLine);
            body.append('\n');
        }
    } catch (IOException e) {
        return null;
    }

    return body.toString();
}

From source file:Main.java

public static InputStream getBmpInputStream(Resources res, int resID) {
    if (res == null || resID == 0) {
        return null;
    }/*w ww . j  av a 2s .c om*/
    return res.openRawResource(resID);
}

From source file:org.lucasr.layoutsamples.util.RawResource.java

public static JSONArray getAsJSON(Context context, int id) throws IOException {
    InputStreamReader reader = null;

    try {/* w  w  w .ja  v  a 2 s . c o  m*/
        final Resources res = context.getResources();
        final InputStream is = res.openRawResource(id);
        if (is == null) {
            return null;
        }

        reader = new InputStreamReader(is);

        final char[] buffer = new char[1024];
        final StringWriter s = new StringWriter();

        int n;
        while ((n = reader.read(buffer, 0, buffer.length)) != -1) {
            s.write(buffer, 0, n);
        }

        return new JSONArray(s.toString());
    } catch (JSONException e) {
        return null;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:Main.java

private static String getStringFromRaw(Context context, int id) {
    String str;// ww w  .ja v  a2 s .  c om
    try {
        Resources r = context.getResources();
        InputStream is = r.openRawResource(id);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            baos.write(i);
            i = is.read();
        }

        str = baos.toString();
        is.close();
    } catch (IOException e) {
        str = "";
    }

    return str;
}

From source file:Main.java

public static CharSequence readFile(Resources resources, int id) {
    BufferedReader in = null;//w  w  w. j  a v a 2  s.co  m
    try {
        in = new BufferedReader(new InputStreamReader(resources.openRawResource(id)), 8192);
        String line;
        StringBuilder buffer = new StringBuilder();
        while ((line = in.readLine()) != null) {
            buffer.append(line).append('\n');
        }
        // Chomp the last newline
        if (buffer.length() > 0) {
            buffer.deleteCharAt(buffer.length() - 1);
        }
        return buffer;
    } catch (IOException e) {
        return "";
    } finally {
        closeStream(in);
    }
}

From source file:Main.java

public static List<String> fileReader(Context context, int rawId) {
    Resources myRes = context.getResources();
    String data = null;//from   ww w.  j  a v a  2s  .  c o  m
    List<String> list = new ArrayList<String>();

    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(myRes.openRawResource(rawId)));

        do {
            data = br.readLine();
            if (data == null) {
                break;
            }
            list.add(data);
            //Log.i(TAG, data);
        } while (true);

    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    }

    return list;
}

From source file:com.mk4droid.IMC_Utils.GEO.java

/**
 * Draw polygon borders on the map defining the municipality
 * /*  w w w  .ja  va 2 s  . co  m*/
 * @param mgmap
 * @param resources
 */
public static Polygon MakeBorders(GoogleMap mgmap, Resources res) {

    String str = "";
    // parse from raw.polygoncoords.txt
    try {
        InputStream in_s = res.openRawResource(R.raw.polygoncoords);
        byte[] b = new byte[in_s.available()];
        in_s.read(b);
        str = new String(b);
    } catch (Exception e) {
        Log.e("Error", "can't read polygon.");
    }

    Polygon mPoly = null;

    if (options == null) {
        if (str.length() > 0) {
            String[] points = str.split(" ");
            options = new PolygonOptions();
            for (int i = 0; i < points.length; i = i + 2)
                options.add(new LatLng(Double.parseDouble(points[i + 1]), Double.parseDouble(points[i])));
        }
    }

    if (mgmap != null)
        mPoly = mgmap.addPolygon(
                options.strokeWidth(4).strokeColor(Color.BLACK).fillColor(Color.argb(10, 0, 100, 0)));

    return mPoly;
}

From source file:android.support.v7.graphics.drawable.VectorDrawableCompat.java

public static VectorDrawableCompat createFromResource(Resources res, int id) {
    XmlPullParserFactory xppf = null;/*from www  .j  a v  a  2s  . c  o  m*/
    XmlPullParser parser = null;
    try {
        xppf = XmlPullParserFactory.newInstance();
        parser = xppf.newPullParser();
        InputStream is = res.openRawResource(id);
        parser.setInput(is, null);
        // TODO: Use this getXml when the aapt is able to help us to keep the
        // attributes for v-21 in the compiled version.
        // XmlPullParser parser = res.getXml(id);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        final VectorDrawableCompat drawable = new VectorDrawableCompat();
        drawable.inflateInternal(res, parser, attrs);
        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOG_TAG, "XmlPullParser exception for res id : " + id);
    }
    return null;
}