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 getSDImg(Context context, String imagePath) {
    if (context == null || imagePath == null) {
        return null;
    }//w  ww .ja v a2  s . co  m

    Bitmap bitmap = null;
    InputStream is = null;
    try {
        File file = new File(imagePath);
        if (!file.exists()) {
            return null;
        }
        if (file.isDirectory()) {
            return null;
        }
        imagePath = "file://" + imagePath;
        Log.i("info", imagePath);
        is = context.getContentResolver().openInputStream(Uri.parse(imagePath));
        bitmap = BitmapFactory.decodeStream(is);
    } catch (FileNotFoundException e) {

    } catch (OutOfMemoryError e) {

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {

            }
        }
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap loadBitmap(Context mContext, int id) {
    final InputStream is = mContext.getResources().openRawResource(id);
    Bitmap bitmap = null;/*from ww  w  .  j ava 2  s  . c o  m*/
    try {
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch (final IOException e) {
            // Ignore.
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap loadBitmap(Context mContext, int id) {
    InputStream is = mContext.getResources().openRawResource(id);
    Bitmap bitmap = null;//w w w . ja va  2 s .  com
    try {
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // Ignore.
        }
    }
    return bitmap;
}

From source file:Main.java

private static Bitmap getIconFromUri(Context mContext, Uri uri) throws IOException {
    InputStream input = mContext.getContentResolver().openInputStream(uri);
    return BitmapFactory.decodeStream(input);
}

From source file:Main.java

/**
 * Get contact photo bitmap.//from  ww w . j  a v a2 s. c om
 *
 * @param context context.
 * @param photo photo uri.
 * @return bitmap.
 */
public static Bitmap getContactBitmapFromURI(Context context, String photo) {
    // Get photo uri.
    Uri uri = Uri.parse(photo);

    try {
        // Get input.
        InputStream input = context.getContentResolver().openInputStream(uri);

        if (input == null) {
            return null;
        }

        return getRoundedCornerBitmap(BitmapFactory.decodeStream(input));
    } catch (FileNotFoundException ignored) {
    }

    return null;
}

From source file:Main.java

public static Bitmap getImageFromURL(URL url) {

    Bitmap bitmap = null;/*  w  w w  .  j  a  v  a  2s. c om*/

    HttpURLConnection connection = null;

    try {

        connection = (HttpURLConnection) url.openConnection();

        if (connection.getResponseCode() != 200) {
            return null;
        }
        if (!CONTENT_TYPE_IMAGE.equalsIgnoreCase(connection.getContentType().substring(0, 5))) {
            return null;
        }

        bitmap = BitmapFactory.decodeStream(connection.getInputStream());

    } catch (IOException e) {

        e.printStackTrace();

    }

    if (connection != null) {

        connection.disconnect();

    }

    return bitmap;

}

From source file:Main.java

public static Bitmap getBitmap(Context context, String imageFile) {
    try {/*from w ww  .j a v a  2  s  .  c  o  m*/
        if (imageFile != null) {
            if (imageFile.contains("/")) {
                imageFile = imageFile.substring(imageFile.lastIndexOf("/") + 1);
            }
            FileInputStream in = context.openFileInput(imageFile);
            return BitmapFactory.decodeStream(in);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap getImage(Context context, String path) {
    AssetManager as = context.getResources().getAssets();

    InputStream is = null;/* ww  w  .ja v  a2  s . co m*/
    Bitmap bitmap = null;
    try {
        is = as.open(path);
        bitmap = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        return null;
    }

    return bitmap;
}

From source file:Main.java

/**
 * Returns a bitmap from a gallery Uri//from  w w w. j a  v a 2 s.co  m
 *
 * @param pContext
 *        Context required to access the content resolver
 * @param pIntent
 *        The Uri of the picker image
 * @return The picked image as a bitmap
 */
public static Bitmap getBitmapFromIntent(Context pContext, Intent pIntent) {
    Bitmap bitmapPickedImage = null;

    Uri pickedImageUri = pIntent.getData();

    // If the URI is not null try to decode it to a bitmap else try to get the bitmap data from the intent
    // http://stackoverflow.com/questions/17123083/null-pointer-exception-while-taking-pictures-from-camera-android-htc
    if (pickedImageUri != null) {
        try {
            InputStream imageStream = pContext.getContentResolver().openInputStream(pickedImageUri);
            bitmapPickedImage = BitmapFactory.decodeStream(imageStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        if (pIntent.getExtras() != null && pIntent.getExtras().get("data") instanceof Bitmap) {
            bitmapPickedImage = (Bitmap) pIntent.getExtras().get("data");
        }
    }

    return bitmapPickedImage;
}

From source file:Main.java

public static Bitmap loadBitmapAsset(Context context, String asset) {
    InputStream is = null;// w  w w  .  java2 s  . c o m
    Bitmap bitmap = null;
    try {
        is = context.getAssets().open(asset);
        if (is != null) {
            bitmap = BitmapFactory.decodeStream(is);
        }
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "Cannot close InputStream: ", e);
            }
        }
    }
    return bitmap;
}