获取图片的缩略图

  1. 获取SD卡所有的jpeg,png格式的图片
public static ArrayList getAllPhotoUrlsFromCR(Context context) {
        ArrayList allPhotoUrls = new ArrayList();


        Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver mContentResolver = context.getContentResolver();

        Cursor mCursor = mContentResolver.query(mImageUri, null,
                MediaStore.Images.Media.MIME_TYPE + "=? or "
                        + MediaStore.Images.Media.MIME_TYPE + "=?",
                new String[] { "image/jpeg", "image/png" },
                MediaStore.Images.Media.DATE_MODIFIED);

        while (mCursor.moveToNext()) {
            String path = mCursor.getString(mCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
            allPhotoUrls.add(path);
        }
        mCursor.close();
        return allPhotoUrls;
    }
  1. 获取缩略图
    1. 1.
public static Bitmap getBitMap_thumbnail_size(String path) {
        float expectWidth = 480f;
        float expectHeigth = 800f;

        // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小,且图片不被加载到内存
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // 调用上面定义的方法计算inSampleSize值
        int height = options.outHeight;
        int width = options.outWidth;
        options.inSampleSize = (int) Math.max(height / expectHeigth, width / expectWidth);

        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);

        return bitmap;
    }
  1. 2.
public static Bitmap getBitMap_thumbnail_quality(String path) {
        int expectWidth = 480;
        int expectHeigth = 800;

        int qualitySize = 30;

        final BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inJustDecodeBounds = true;  
        BitmapFactory.decodeFile(path, options);  

        // Calculate inSampleSize  
        options.inSampleSize = calculateInSampleSize(options,expectWidth,expectHeigth);

        // Decode bitmap with inSampleSize set  
        options.inJustDecodeBounds = false;  

        Bitmap bm = BitmapFactory.decodeFile(path, options); 

        try {
            ByteArrayOutputStream baos = null ;  
            baos = new ByteArrayOutputStream();  
            bm.compress(Bitmap.CompressFormat.JPEG, qualitySize, baos); 
            byte[] bytes = baos.toByteArray();
            baos.close();
            bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bm;
    }

    private static int calculateInSampleSize(BitmapFactory.Options options,  
            int reqWidth, int reqHeight) {  
        final int height = options.outHeight;  
        final int width = options.outWidth;  
        int inSampleSize = 1;  

        if (height > reqHeight || width > reqWidth) {  
            final int heightRatio = Math.round((float) height  
                    / (float) reqHeight);  
            final int widthRatio = Math.round((float) width / (float) reqWidth);  

            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;  
        }  
        return inSampleSize;  
    }  

你可能感兴趣的:(获取图片的缩略图)