Example usage for android.graphics BitmapFactory decodeByteArray

List of usage examples for android.graphics BitmapFactory decodeByteArray

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeByteArray.

Prototype

public static Bitmap decodeByteArray(byte[] data, int offset, int length) 

Source Link

Document

Decode an immutable bitmap from the specified byte array.

Usage

From source file:Main.java

public static Bitmap GetLocalOrNetBitmap(String url) {
    Bitmap bitmap = null;/*  w w w . j  a  va2s  .c o m*/
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(url).openStream(), 1024);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024);
        copy(in, out);
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static byte[] resizeImage(byte[] input, int length) {
    Bitmap original = BitmapFactory.decodeByteArray(input, 0, input.length);
    Bitmap resized = Bitmap.createScaledBitmap(original, length, length, true);

    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);

    return blob.toByteArray();
}

From source file:Main.java

public static Bitmap readBitmapAttribute(XmlPullParser in, String name) {
    final byte[] value = readByteArrayAttribute(in, name);
    if (value != null) {
        return BitmapFactory.decodeByteArray(value, 0, value.length);
    } else {//from  w  w  w  .  j a  va  2  s  .c om
        return null;
    }
}

From source file:Main.java

public static Bitmap loadImageFromUrl(String url) {
    ByteArrayOutputStream out = null;
    Bitmap bitmap = null;//  ww  w .  j  av a  2s.  c o m
    int BUFFER_SIZE = 1024 * 8;
    try {
        BufferedInputStream in = new BufferedInputStream(new URL(url).openStream(), BUFFER_SIZE);
        out = new ByteArrayOutputStream(BUFFER_SIZE);
        int length = 0;
        byte[] tem = new byte[BUFFER_SIZE];
        length = in.read(tem);
        while (length != -1) {
            out.write(tem, 0, length);
            out.flush();
            length = in.read(tem);
        }
        in.close();
        if (out.toByteArray().length != 0) {
            bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
        } else {
            out.close();
            return null;
        }
        out.close();
    } catch (OutOfMemoryError e) {

        out.reset();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2;
        opts.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size(), opts);
        return bitmap;
    } catch (Exception e) {

        return bitmap;
    }
    return bitmap;
}

From source file:Main.java

/**
 * Converts given array of bytes to {@link Bitmap}
 *
 * @param imageBlob array of bytes with raw image data
 *
 * @return {@link Bitmap} created from <tt>imageBlob</tt>
 */// w  w w  .  j a v a 2  s .  c o m
static public Bitmap bitmapFromBytes(byte[] imageBlob) {
    if (imageBlob != null) {
        return BitmapFactory.decodeByteArray(imageBlob, 0, imageBlob.length);
    }
    return null;
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlString) {
    Bitmap bitmap = null;//  www .  ja  va2  s. c om
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(urlString).openStream(), 1024 * 4);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024 * 4);
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getAndSetBitmapFromNet(String urlPath) {

    Bitmap bm = null;//w ww. j a v a  2 s.c o m

    if (urlPath != null) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024);
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024);
            copy(bis, out);
            out.flush();
            final byte[] data = dataStream.toByteArray();
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            Log.i(I, "data.length: " + data.length);
            out.close();
            dataStream.close();
            bis.close();
            bm = processBitmap(bm);
        } catch (IOException e) {
            Log.i(I, "URL Connection or Bitmap processing Exception");
            e.printStackTrace();
        }
    }
    return bm;
}

From source file:Main.java

/**
 * Decode bitmap from byte array//  ww w .  jav  a  2  s . com
 *
 * @param bytes the byte array
 * @return the bitmap
 */
public static Bitmap getBitmap(@NonNull byte[] bytes) {
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}

From source file:Main.java

/**
 * Decodes a string into a Bitmap//  ww w .  j  a va 2  s . c o m
 *
 * @param input String to decode
 * @return decoded Bitmap
 */
private static Bitmap decodeBitmap(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

From source file:Main.java

/**
 * From original Android Camera App//from  w  w w  . ja v a 2s.  com
 * See: http://android.git.kernel.org/?p=platform/packages/apps/Camera.git;a=blob;f=src/com/android/camera/ImageManager.java;h=76a6d1dffdcfb91f2c55032ce14f7cd9ecf7962c;hb=HEAD
 * @param imageData
 * @param quality
 * @param filename
 * @return
 */
public static boolean StoreByteImage(byte[] imageData, int quality, String filename) {
    String directory = storageTarget + "/FreqCap";
    Bitmap source = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

    OutputStream outputStream = null;
    String filePath = directory + "/" + filename;
    try {
        File dir = new File(directory);
        if (!dir.exists())
            dir.mkdirs();
        File file = new File(directory, filename);
        outputStream = new FileOutputStream(file);
        if (source != null) {
            source.compress(CompressFormat.JPEG, picCompression, outputStream);
        } else {
            outputStream.write(imageData);
            // degree[0] = getExifOrientation(filePath);
        }
        //         outputStream.flush();
        //         outputStream.close();
        Log.e(TAG, file.getAbsolutePath());
    } catch (FileNotFoundException ex) {
        Log.w(TAG, ex);
        return false;
    } catch (IOException ex) {
        Log.w(TAG, ex);
        return false;
    } finally {
        closeSilently(outputStream);
    }
    return true;
}