get Assets Image Byte by file name - Android App

Android examples for App:Assets Image

Description

get Assets Image Byte by file name

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.AssetManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static byte[] getAssetsImageByte(Context context, String fileName) {

        byte[] image;
        AssetManager am = context.getResources().getAssets();
        try {//from w w w  . jav  a 2  s .c  om
            InputStream is = am.open(fileName);
            image = InputStreamToByte(is);
            is.close();
            return image;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    private static byte[] InputStreamToByte(InputStream is)
            throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    }
}

Related Tutorials