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, Options opts) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:com.nd.pad.GreenBrowser.util.ImageDownloader.java

/**
  * //w ww.  j  a va  2 s.c o m
  *  @ : getBitmapFromLocal
  *  @ :  
  *  @
  *      @param url 
  *      @return 
  *
  *  @
  *  @2013-1-5 4:19:23  
  *  @ 
  *
  */
public Bitmap getBitmapFromLocal(String url, String parentFullFileName) {
    try {
        String filename = parentFullFileName + File.separator + convertUrlToFileName(url);
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(filename, opt);
        if (bitmap != null) {
            sHardBitmapCache.put(convertUrlToFileName(url), bitmap);
        }
        return bitmap;
    } catch (Exception e) {
        return null;
    }

}

From source file:com.c4mprod.utils.ImageManager.java

public static Bitmap shrinkBitmap(String file, int width, int height) {
    try {/*  ww  w  .j a  v a  2  s. c o m*/
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        bmpFactoryOptions.inPurgeable = true;
        bmpFactoryOptions.inInputShareable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
        computeRatio(width, height, bmpFactoryOptions);
        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
        return bitmap;
    } catch (OutOfMemoryError e) {
        // Log.d("ImprovedImageDownloader.shrinkBitmap():", "memory !");
        return null;
    }
}

From source file:com.nd.pad.GreenBrowser.util.ImageDownloader.java

/**
 * //from   w ww. j  a v a2s  .c om
 * 
 * @param filename 
 * @param width 
 * @param height 
 * @return 
 *  MyGoPlusV3 
 * <pre>
 *             
 * 2013-6-14   ?      
 * </pre>
 */
public Bitmap loadResizedBitmap(String filename, double width, double height) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    BitmapFactory.decodeFile(filename, options);
    if (options.outHeight > 0 && options.outWidth > 0) {
        options.inJustDecodeBounds = false;
        if (options.outWidth > width && options.outHeight > height) {
            double scaleWidth = options.outWidth / width;
            double scaleHeight = options.outHeight / height;
            double minScale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
            options.inSampleSize = (int) minScale;
        }

        bitmap = BitmapFactory.decodeFile(filename, options);
    }
    return bitmap;
}

From source file:com.safecell.HomeScreenActivity.java

Bitmap getImageFromURI(Uri uri) {
    Bitmap resizedBitmap = null;//  ww  w .  j  av  a2s.c om
    String abc = null;
    if (uri != null) {
        String str = uri.toString();
        abc = str.substring(0, 1);
        // Log.v("Safecell :" + "abc", str);
    }

    if (uri != null && abc.equalsIgnoreCase("c")) {
        Uri selectedImage = uri;
        // Log.v("Safecell :" + "Uri", selectedImage.toString());

        String[] proj = { MediaColumns.DATA };
        Cursor cursor = managedQuery(selectedImage, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();

        String path = cursor.getString(column_index);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        resizedBitmap = BitmapFactory.decodeFile(path, options);
        imageStoreInDatabase(resizedBitmap);

        cursor.close();

        return resizedBitmap;
    } else if (uri != null && abc.equalsIgnoreCase("/")) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        resizedBitmap = BitmapFactory.decodeFile(uri.getPath(), options);
        imageStoreInDatabase(resizedBitmap);
        return resizedBitmap;

    }
    return resizedBitmap;

}

From source file:com.xmobileapp.rockplayer.LastFmAlbumArtImporter.java

/*********************************
 * //from w  ww  . j a  va2  s  .  c o  m
 * creatAlbumArt
 * 
 *********************************/
public Bitmap createAlbumArt(String artistName, String albumName, String albumArtURL) {
    String fileName = ((RockPlayer) context).FILEX_ALBUM_ART_PATH + validateFileName(artistName) + " - "
            + validateFileName(albumName) + FILEX_FILENAME_EXTENSION;
    validateFileName(fileName);
    File albumArtFile = new File(fileName);
    try {
        /*
         * Retreive Album
         */

        albumArtFile.createNewFile();
        FileOutputStream albumArtFileStream = new FileOutputStream(albumArtFile);

        /*
         * retreive URL
         */
        BasicHttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 10000);
        DefaultHttpClient httpClient = new DefaultHttpClient(params);
        //      DefaultedHttpParams params = new DefaultedHttpParams(httpClient.getParams(),
        //            httpClient.getParams());
        //      httpClient.setParams(params);
        HttpGet httpGet = new HttpGet(albumArtURL);
        HttpResponse response;
        HttpEntity entity;
        InputStream albumArtURLStream = null;

        response = httpClient.execute(httpGet);
        entity = response.getEntity();
        //         BufferedReader in = 
        //            new BufferedReader(new InputStreamReader(
        //                  entity.getContent()));
        albumArtURLStream = entity.getContent();

        //      URL albumArt = new URL(albumArtURL);
        //      InputStream albumArtURLStream = albumArt.openStream();

        byte buf[] = new byte[1024];
        int len;
        while ((len = albumArtURLStream.read(buf)) >= 0)
            albumArtFileStream.write(buf, 0, len);

        albumArtFileStream.close();
        albumArtURLStream.close();

        /*
         * Rescale/Crop the Bitmap
         */
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap albumArtBitmap = BitmapFactory.decodeFile(fileName, opts);
        if (opts.outHeight < 320 || opts.outWidth < 320)
            return null;
        int MAX_DIM = 460;
        int sampling = 1;
        if (Math.min(opts.outWidth, opts.outHeight) > MAX_DIM) {
            sampling = (int) Math.floor(Math.min(opts.outWidth, opts.outHeight) / MAX_DIM);
        }
        opts.inSampleSize = sampling;
        opts.inJustDecodeBounds = false;
        FileInputStream fileNameStream = new FileInputStream(fileName);
        albumArtBitmap = BitmapFactory.decodeStream(fileNameStream, null, opts);
        fileNameStream.close();

        if (albumArtBitmap != null) {
            int dimension = Math.min(480, Math.min(albumArtBitmap.getHeight(), albumArtBitmap.getWidth()));
            Bitmap albumArtBitmapRescaled = Bitmap.createBitmap(albumArtBitmap,
                    (int) Math.floor((albumArtBitmap.getWidth() - dimension) / 2),
                    (int) Math.floor((albumArtBitmap.getHeight() - dimension) / 2), (int) dimension,
                    (int) dimension);

            FileOutputStream rescaledAlbumArtFileStream;
            rescaledAlbumArtFileStream = new FileOutputStream(albumArtFile);
            albumArtBitmapRescaled.compress(Bitmap.CompressFormat.JPEG, 96, rescaledAlbumArtFileStream);
            rescaledAlbumArtFileStream.close();
            if (albumArtBitmapRescaled != null)
                albumArtBitmapRescaled.recycle();

        } else {
            albumArtFile.delete();
        }
        if (albumArtBitmap != null)
            albumArtBitmap.recycle();
        return albumArtBitmap;
    } catch (Exception e) {
        e.printStackTrace();
        albumArtFile.delete();
        return null;
    } catch (Error err) {
        err.printStackTrace();
        return null;
    }
}

From source file:com.eleybourn.bookcatalogue.utils.Utils.java

/**
 * If there is a '__thumbnails' key, pick the largest image, rename it
 * and delete the others. Finally, remove the key.
 * //  w  w  w  .j  av  a2  s .  c o m
 * @param result   Book data
 */
static public void cleanupThumbnails(Bundle result) {
    if (result.containsKey("__thumbnail")) {
        long best = -1;
        int bestFile = -1;

        // Parse the list
        ArrayList<String> files = Utils.decodeList(result.getString("__thumbnail"), '|');

        // Just read the image files to get file size
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;

        // Scan, finding biggest
        for (int i = 0; i < files.size(); i++) {
            String filespec = files.get(i);
            File file = new File(filespec);
            if (file.exists()) {
                BitmapFactory.decodeFile(filespec, opt);
                // If no size info, assume file bad and skip
                if (opt.outHeight > 0 && opt.outWidth > 0) {
                    long size = opt.outHeight * opt.outWidth;
                    if (size > best) {
                        best = size;
                        bestFile = i;
                    }
                }
            }
        }

        // Delete all but the best one. Note there *may* be no best one,
        // so all would be deleted. We do this first in case the list 
        // contains a file with the same name as the target of our
        // rename.
        for (int i = 0; i < files.size(); i++) {
            if (i != bestFile) {
                File file = new File(files.get(i));
                file.delete();
            }
        }
        // Get the best file (if present) and rename it.
        if (bestFile >= 0) {
            File file = new File(files.get(bestFile));
            file.renameTo(CatalogueDBAdapter.getTempThumbnail());
        }
        // Finally, cleanup the data
        result.remove("__thumbnail");
        result.putBoolean(CatalogueDBAdapter.KEY_THUMBNAIL, true);
    }
}

From source file:com.xmobileapp.rockplayer.LastFmAlbumArtImporter.java

/*********************************
 * //  ww w .ja  v a2 s  . c  om
 * albumHasArt
 *
 *********************************/
private String getAlbumArtPath(String artistName, String albumName) {

    String albumCoverPath = null;

    /*
      * 1. Check if we have downloaded it before
      */
    //       if(albumCoverPath == null){
    String path = ((RockPlayer) context).FILEX_ALBUM_ART_PATH + validateFileName(artistName) + " - "
            + validateFileName(albumName) + FILEX_FILENAME_EXTENSION;
    File albumCoverFilePath = new File(path);
    if (albumCoverFilePath.exists() && albumCoverFilePath.length() > 0) {
        albumCoverPath = albumCoverFilePath.getAbsolutePath();
    }
    //       }

    /*
     * 2. Check Art in the DB
     */
    if (albumCoverPath == null) {
        albumCoverPath = albumCursor
                .getString(albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART));

        /* check if the embedded mp3 is valid (or big enough)*/
        if (albumCoverPath != null) {
            Log.i("DBG", albumCoverPath);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            Bitmap bmTmp = BitmapFactory.decodeFile(albumCoverPath, opts);
            if (opts == null || opts.outHeight < 320 || opts.outWidth < 320)
                albumCoverPath = null;
            if (bmTmp != null)
                bmTmp.recycle();
        }
    }

    Log.i("DBG", "" + albumCoverPath);

    /*
     * If both checks above have failed return false
     */
    return albumCoverPath;
}

From source file:org.opendatakit.common.android.utilities.ODKFileUtils.java

public static Bitmap getBitmapScaledToDisplay(String appName, File f, int screenHeight, int screenWidth) {
    // Determine image size of f
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;/*ww w . j a  v a  2  s .c o  m*/
    BitmapFactory.decodeFile(f.getAbsolutePath(), o);

    int heightScale = o.outHeight / screenHeight;
    int widthScale = o.outWidth / screenWidth;

    // Powers of 2 work faster, sometimes, according to the doc.
    // We're just doing closest size that still fills the screen.
    int scale = Math.max(widthScale, heightScale);

    // get bitmap with scale ( < 1 is the same as 1)
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = scale;
    Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
    if (b != null) {
        WebLogger.getLogger(appName).i(t, "Screen is " + screenHeight + "x" + screenWidth
                + ".  Image has been scaled down by " + scale + " to " + b.getHeight() + "x" + b.getWidth());
    }
    return b;
}

From source file:com.xmobileapp.rockplayer.LastFmAlbumArtImporter.java

/*******************************
 * //from w  w  w.jav a  2  s . c  o  m
 * checkAlbumArtEmbeddedSize
 * 
 *******************************/
public String checkAlbumArtEmbeddedSize(String albumCoverPath) {

    /* check if the embedded mp3 is valid (or big enough)*/
    if (albumCoverPath != null) {
        Log.i("DBG", albumCoverPath);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap bmTmp = BitmapFactory.decodeFile(albumCoverPath, opts);
        if (opts == null || opts.outHeight < 320 || opts.outWidth < 320)
            albumCoverPath = null;
        if (bmTmp != null)
            bmTmp.recycle();
    }

    return albumCoverPath;

}

From source file:com.kjsaw.alcosys.ibacapp.IBAC.java

private Bitmap readPhotos() {
    String localPath = getApplicationContext().getFilesDir().getAbsolutePath() + "/photos/";
    String fileName = localPath + "Test.jpg";

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*w  w w  . j  a  va2 s  .c om*/
    BitmapFactory.decodeFile(fileName, options);
    options.inSampleSize = computeSampleSize(options, -1, 128 * 128);
    options.inJustDecodeBounds = false;
    try {
        return BitmapFactory.decodeFile(fileName, options);
    } catch (OutOfMemoryError err) {
        err.printStackTrace();
    }
    return null;
}