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 getBitmapFromUrl(String imageUrl) {
    Bitmap bmp = null;//from w ww  .  j  a  v a 2s  . c o m
    try {
        if (!TextUtils.isEmpty(imageUrl) && imageUrl.startsWith("http")) {
            URL url = new URL(imageUrl);
            bmp = BitmapFactory.decodeStream(url.openStream());
        } else {
            bmp = BitmapFactory.decodeFile(imageUrl);
        }
    } catch (Throwable ex) {

    }
    return bmp;
}

From source file:Main.java

public static Bitmap decodeUriToBitmap(Context context, Uri uri) {
    if (context == null || uri == null)
        return null;

    Bitmap bitmap = null;/*from  w  w  w .j ava2s  . c o  m*/
    try {
        bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmapByFile(File file) {
    FileInputStream fis = null;//from w ww.ja  va2 s  .  co m
    Bitmap bitmap = null;
    try {
        fis = new FileInputStream(file);
        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

public static Bitmap getLoacalBitmap(String url) {
    try {/*  w w w. j a  va 2  s  .  co m*/
        if (null == url) {
            return null;
        } else {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getRemoveBitmap(Context context, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from  w  w w. j a  v  a2s  .c o  m*/
    Bitmap bitmap1;
    try {
        Bitmap bitmap2 = BitmapFactory.decodeStream(context.getAssets().open("remove@2x.png"));
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap1);
        canvas1.drawARGB(0, 0, 0, 0);
        canvas1.drawBitmap(bitmap, bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, paint);
        bitmap.recycle();
        canvas1.drawBitmap(bitmap2, 0.0F, 0.0F, paint);
        bitmap2.recycle();
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
    }
    return bitmap1;
}

From source file:Main.java

private static void loadAssetImage(ImageView view, String filename, int defaultIconId) {

    InputStream input = null;//  w w w. j a v  a2 s  .  c om
    try {
        input = view.getContext().getAssets().open(filename);
        Bitmap icon = BitmapFactory.decodeStream(input);
        view.setImageBitmap(icon);
        input.close();
    } catch (Exception error) {
        view.setImageResource(defaultIconId);
    } finally {
        silentClose(input);
    }
}

From source file:Main.java

public static Bitmap getBitmapFromUri(@NonNull Context context, @NonNull Uri uri) throws IOException {
    InputStream stream = context.getContentResolver().openInputStream(uri);
    Bitmap rawImage = BitmapFactory.decodeStream(stream);

    try {//from  ww  w .j ava  2  s .com
        stream.close();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    return rawImage;
}

From source file:Main.java

public static Bitmap getBitmap(InputStream is) {
    try {/*from   w w w. j av a2s .c om*/
        return BitmapFactory.decodeStream(is);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static int loadTexture(String s) {
    Bitmap img;/*from  w  w  w.j a v a 2 s . co m*/
    try {
        img = BitmapFactory.decodeStream(context.getAssets().open(s));
    } catch (IOException e) {
        return -1;
    }
    int tex[] = new int[1];
    GLES20.glGenTextures(1, tex, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex[0]);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, img, 0);
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    img.recycle();
    return tex[0];
}

From source file:Main.java

/**
 * Load an image from an asset file./*  w ww  .  j  a va 2s.c  o m*/
 *
 * @param assetManager
 * @param imageAssetPath Path to image
 * @return image object
 * @throws IOException
 */
public static Bitmap loadBitmapAsset(AssetManager assetManager, String imageAssetPath) throws IOException {
    return BitmapFactory.decodeStream(assetManager.open(imageAssetPath));
}