Example usage for android.graphics BitmapFactory decodeStream

List of usage examples for android.graphics BitmapFactory decodeStream

Introduction

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

Prototype

public static Bitmap decodeStream(InputStream is) 

Source Link

Document

Decode an input stream into a bitmap.

Usage

From source file:Main.java

public static Bitmap getBitMap(String url) {
    URL myFileUrl = null;//w w w.  j  a  v a 2s  .c o m
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap load(String filepath) {
    Bitmap bitmap = null;//w w w .j  ava 2 s .com
    try {
        FileInputStream fin = new FileInputStream(filepath);
        bitmap = BitmapFactory.decodeStream(fin);
        fin.close();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    return bitmap;
}

From source file:Main.java

public static Bitmap returnBitMap(String url) {
    URL myFileUrl = null;//from  ww w  . java 2s.  c om
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Log.v(tag, bitmap.toString());

    return bitmap;
}

From source file:Main.java

public static Bitmap returnBitmap(String url) {
    URL fileUrl = null;/*from  w  ww.ja va  2s  .c om*/
    Bitmap bitmap = null;

    try {
        fileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;

}

From source file:Main.java

/**
 * Convert an image to a byte array//  w  w w .ja  v a2  s  .com
 * @param uri
 * @param context
 * @return
 */
public static byte[] convertImageToByte(Uri uri, Context context) {
    byte[] data = null;
    try {
        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = cr.openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        data = baos.toByteArray();
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e2) {
            }
        }
        if (baos != null) {
            try {
                baos.close();
            } catch (Exception e2) {
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

/**
 * @param context//from  www .j  ava2s .  co m
 * @param data
 * @return
 */
@Nullable
public static byte[] retrieveSelectedImage(@NonNull Context context, @NonNull Intent data) {
    InputStream inStream = null;
    Bitmap bitmap = null;
    try {
        inStream = context.getContentResolver().openInputStream(data.getData());
        bitmap = BitmapFactory.decodeStream(inStream);
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        return outStream.toByteArray();
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException ignored) {
            }
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}

From source file:Main.java

/**
 * download and return bitmap for the given url
 * DO not call this method from main thread
 * @param src/*  w w  w. j a  v a 2 s  .c  o m*/
 * @return
 */
public static Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        if (myBitmap != null) {
            myBitmap = getResizedBitmap(myBitmap, 200, 200);
        }
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getHttpBitmap(String url) {
    URL myFileUrl = null;//from  w  ww .  ja va  2s  .c  o  m
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setConnectTimeout(0);
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmap(Context context, String fileName) {
    FileInputStream fis = null;//from  www . j  a  v  a2 s. com
    Bitmap bitmap = null;
    try {
        fis = context.openFileInput(fileName);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException | OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

/**
 * Get the contact photo from a contact URI.
 *
 * @param context The context.//from  ww w .ja v  a  2  s .c  om
 * @param contactUri The contact URI.
 *
 * @return The contact photo.
 */
public static Bitmap getContactPhoto(final Context context, Uri contactUri) {
    Bitmap bitmap = null;
    if (contactUri != null) {
        InputStream inputStream = ContactsContract.Contacts
                .openContactPhotoInputStream(context.getContentResolver(), contactUri);
        if (inputStream != null) {
            bitmap = BitmapFactory.decodeStream(inputStream);
            try {
                inputStream.close();
            } catch (IOException e) {

            }
        }
    }

    return bitmap;
}