从 Uri中获取bitmap

        BitmapFactory.Options options = new BitmapFactory.Options();
        InputStream input = null;
        Bitmap bitmap = null;
        try {
            input = context.getContentResolver().openInputStream(uri);
            options.inJustDecodeBounds = true;
            options.inDither = true;//optional
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
            BitmapFactory.decodeStream(input, null, options);
            input.close();
            int height = options.outHeight;
            int width = options.outWidth;
            Log.i("getbitmap1",height+" "+width+" "+getWidth()+" "+getHeight());
            // 调用上面定义的方法计算inSampleSize值  calculateInSampleSize是一个方法计算inSampleSize
            options.inSampleSize = calculateInSampleSize(options,w,h);
            // 使用获取到的inSampleSize值再次解析图片
            options.inJustDecodeBounds = false;
            input = context.getContentResolver().openInputStream(uri);
            bitmap = BitmapFactory.decodeStream(input, null, options);
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(从 Uri中获取bitmap)