Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

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

Prototype

public static Bitmap decodeFile(String pathName) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:Main.java

public static Bitmap decodeBitmapFromAlbumId(Context context, long album_id) {
    String pathName = getRealPathFromURI(context, album_id);
    return BitmapFactory.decodeFile(pathName);
}

From source file:Main.java

/**
 * Encode an image form the given path into a {@link Base64}  string
 *
 * @param imagePath Path of the image to encode (must be absolute)
 * @return a {@link Base64} encoded string.
 *//*from w w  w.  j  a v a  2 s. com*/
public static String encodeBitmap(String imagePath) {
    Bitmap image = BitmapFactory.decodeFile(imagePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] b = bos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

/**
 * Decodes an image File to Bitmap without resizing the image.<br>
 * Be careful, if the image is big this can become a problem because it will use to much memory.
 * @param file the image file reference/*  www. ja va 2 s. co m*/
 * @return the Bitmap
 */
public static Bitmap decodeBitmapFromFile(File file) {
    return BitmapFactory.decodeFile(file.getPath());
}

From source file:Main.java

/**
 * Gets a bitmap from a path inside the TexturePoemApp-Folder
 * @param path path of the bitmap inside the TexturePoemApp-Folder
 * @return Decoded bitmap /*www .  j a  v  a 2s  .  com*/
 */
public static Bitmap getBitmapFromPath(String path) {
    Bitmap b = null;
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String galleryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                .toString();

        File f = new File(galleryPath + "/TexturePoemApp/" + path);
        if (f.exists()) {
            b = BitmapFactory.decodeFile(f.getAbsolutePath());
        }
    }

    return b;
}

From source file:com.angrystone.JpegCompressor.java

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String filename = "/mnt/sdcard/DCIM/Camera/Convert_";
    String orifilename = "/mnt/sdcard/DCIM/Camera/";

    if (action.equals("compress")) {
        Integer quality = 100;/*  w  w w  . ja v  a  2s. c o m*/

        try {
            orifilename = orifilename.concat(args.getString(0));
            filename = filename.concat(args.getString(0));
            quality = args.getInt(1);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        File file = new File(filename);
        Bitmap bitmap = BitmapFactory.decodeFile(orifilename);

        try {
            outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        status = PluginResult.Status.INVALID_ACTION;
    }
    return new PluginResult(status, filename);
}

From source file:MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_RESULT && resultCode == RESULT_OK) {
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(mLastPhotoURI.getPath()));

        if (data != null) {
            imageView.setImageBitmap((Bitmap) data.getExtras().get("data"));

            try {
                imageView.setImageBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(),
                        Uri.parse(data.toUri(Intent.URI_ALLOW_UNSAFE))));
            } catch (IOException e) {
                e.printStackTrace();//from  w  w  w  . ja va 2  s  . c o  m
            }
        }

    }
}

From source file:Main.java

public static Bitmap getCompressBitmap(Bitmap bitmap, File file, int quality, int fileSize) {
    Bitmap bmp = null;/*from  ww  w .j  a v a  2s .  c o  m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    boolean result = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
    LOG("getCompressBitmap result: " + result);
    try {
        if (file != null) {
            if (result) {
                byte[] bt = bos.toByteArray();

                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bt);
                fos.close();

                LOG("file.length(): " + file.length());

                if (file.length() > fileSize) {
                    bmp = getCompressBitmap(bmp, file, (int) (quality * 0.8), fileSize);
                } else {
                    bmp = BitmapFactory.decodeFile(file.getPath());
                }
            }

        } else {
            bmp = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size());
        }
        bos.close();
    } catch (Exception e) {
        LOG("getCompressBitmap result: e" + e.toString());
        e.printStackTrace();
        return null;
    }
    return bmp;
}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

/**
 * Returns a Bitmap of the Gravatar associated with the provided ID. This
 * image will be scaled according to the provided size.
 * //  ww  w.  j a  v a 2  s.  c o m
 * @param id
 * @param size
 * @return a scaled Bitmap
 */
public static Bitmap getGravatar(final String id, final int size) {
    Bitmap bm = null;
    try {
        final File gravatars = ensure_directory(ROOT_DIR);
        /* Prevents the gravatars from showing up in the Gallery app */
        hideMediaFromGallery(gravatars);

        final File image = new File(gravatars, id + ".png");
        bm = BitmapFactory.decodeFile(image.getPath());
        if (bm == null) {
            bm = downloadGravatar(id);
            /* Compress to a 100x100px PNG */
            bm.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(image));
        }
        bm = Bitmap.createScaledBitmap(bm, size, size, true);
    } catch (final IOException e) {
        Log.e("debug", "Error saving bitmap", e);
        e.printStackTrace();
    }
    return bm;
}

From source file:Main.java

public static Bitmap loadFile(String path) {
    try {/* w ww  .j av  a2s .c o m*/
        return BitmapFactory.decodeFile(path);
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:de.dmxcontrol.device.EntityDevice.java

public static Bitmap getDefaultIcon(Context context) {

    File imgFile = new File(FileManager.ImageStorageName + File.separator + defaultDeviceIcon);
    if (imgFile.isFile()) {
        if (imgFile.exists()) {
            return BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        }//ww w.  j  a v  a  2  s . c om
    }

    // Replace this icon with something else
    return BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
}