Android 缩小相机拍摄的照片并获取图片路径

Android 缩小相机拍摄的照片并获取图片路径,android,android-camera,android-image,Android,Android Camera,Android Image,我正在尝试缩放用相机拍摄的照片,但我不知道在哪里或如何做到这一点。现在代码正在访问相机,拍摄一张照片并在listview中显示,我也想获得照片路径,但我不确定如何做到这一点。任何帮助都将不胜感激 /** * This function is called when the add player picture button is clicked. * It accesses the devices gallery and the user can choose a pictu

我正在尝试缩放用相机拍摄的照片,但我不知道在哪里或如何做到这一点。现在代码正在访问相机,拍摄一张照片并在listview中显示,我也想获得照片路径,但我不确定如何做到这一点。任何帮助都将不胜感激

/**
     * This function is called when the add player picture button is clicked.
     * It accesses the devices gallery and the user can choose a picture
     * from the gallery.
     * Or if the user chooses to take a picture with the camera, it handles that
     */

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                this.picPath = selectedImage.getPath();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
                    imageView = (ImageView) findViewById(R.id.imagePlayer); 
                    imageView.setImageBitmap(bitmap);
                    Toast.makeText(this, selectedImage.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }

谢谢

获取位图图像后,您可以使用bitmap类中的createScaledBitmap静态方法

   Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50
但在未来极端记忆不足的情况下。。。 如果不小心,位图可能会快速消耗可用内存预算,导致应用程序因可怕的异常而崩溃: java.lang.OutofMemoryError:位图大小超过VM预算

因此,为了避免java.lang.OutOfMemory异常,请在解码位图之前检查位图的尺寸,除非您完全信任源为您提供可预测大小的图像数据,这些数据可以轻松地放入可用内存中

  // below 3 line of code will come instead of 
//imageView.setImageBitmap(bitmap);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();         
    photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
    imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));  
BitmapFactory类提供了几种解码方法(decodeByteArray()、decodeFile()、decodeResource()等),用于从各种源创建位图。根据图像数据源选择最合适的解码方法。这些方法试图为构造的位图分配内存,因此很容易导致OutOfMemory异常。每种类型的解码方法都有额外的签名,可以通过BitmapFactory.options类指定解码选项。解码时将inJustDecodeBounds属性设置为true可避免内存分配,位图对象返回null,但设置outWidth、outHeight和outMimeType。此技术允许您在构建位图(和内存分配)之前读取图像数据的尺寸和类型

 // please define following two methods in your activity
    public Bitmap decodeSampledBitmapFromByte(byte[] res,
                int reqWidth, int reqHeight) {

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(res, 0, res.length,options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeByteArray(res, 0, res.length,options);
        }
         public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float)height / (float)reqHeight);
                } else {
                    inSampleSize = Math.round((float)width / (float)reqWidth);
                }
            }
            return inSampleSize;
        }
并请参考以下链接从Android培训任何位图相关 java.lang.OutofMemoryError:位图大小超过VM预算

有关缩放,请参见