Android Asset Read assetAsByteArray(Resources res, String path)

Here you can find the source of assetAsByteArray(Resources res, String path)

Description

Load an asset from a resource and return the content as byte array.

Declaration

public static byte[] assetAsByteArray(Resources res, String path)
        throws IOException 

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.Resources;

public class Main {
    /**/*from  w  ww  .j  a v a 2 s . c o  m*/
     * Load an asset from a resource and return the content as byte array.
     */
    public static byte[] assetAsByteArray(Resources res, String path)
            throws IOException {
        InputStream is = res.getAssets().open(path);

        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        byte[] temp = new byte[1024];
        int read;

        while ((read = is.read(temp)) > 0) {
            buf.write(temp, 0, read);
        }
        is.close();
        return buf.toByteArray();
    }
}

Related

  1. getFileFromAssets(Context context, String file)
  2. getImgFromAsserts(Context con, String filename)
  3. listAssetFiles(Context context, String path)
  4. getfromAssets(Context ctx, String file_name)