android 根据图片路径和大小获取缩图

来源:互联网 发布:java.long.illegalargu 编辑:程序博客网 时间:2024/06/07 06:01
 /**
     * 获取缩图
     * 
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static Bitmap getBitmap(String path, int width, int heigth)
    {
        
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try
        {
            BitmapFactory.decodeStream(new FileInputStream(new File(path)), null, options);
        }
        catch (FileNotFoundException e1)
        {
            e1.printStackTrace();
        }


        int width_tmp = options.outWidth;
        int height_tmp = options.outHeight;
        final int minSideLength = Math.min(width_tmp, height_tmp);
        options.inSampleSize = computSize(options, minSideLength, width * heigth);
        options.inJustDecodeBounds = false;
        options.inInputShareable = true;
        // 1
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        try
        {
            // 2
            Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(new File(path)), null, options);
            SoftReference<Bitmap> softBitmap = new SoftReference<Bitmap>(bm);
            return softBitmap.get();
            // return BitmapFactory.decodeFile(path, options);
        }
        catch (OutOfMemoryError e)
        {
            Log.e("ERROR", e.getMessage(), e);
        }
        catch (Exception e)
        {
            Log.e("ERROR", e.getMessage(), e);
        }
        return null;
    }
    
原创粉丝点击