获取图片的缩略图

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


        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;  
    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值