繁体   English   中英

将从图库中选取的图像调整为图像视图的大小

[英]Resize a Image Picked from Gallery to Image View

我的应用程序有一个ImageView活动,我从电话库中选择一张图片,并在此ImageView中设置为用户的个人资料图片。

我的问题是,某些图片在拾取后会使应用程序停止原因太大,我想知道是否有人可以查看我的代码并帮助我如何调整此拾取图片的大小,因此在此图像视图中设置后,用户如何裁剪该图片设置之前的图片,下面是我的图片图片代码。 如果有人进行必要的更改并给我代码,我将非常感激,因为我对开发尚不甚了解。 谢谢。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.User);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

    }

不会重写您的代码,但这可能很有用

    Bitmap bitmap = BitmapFactory.decodeFile(foto.getFotoOrderFilePath());
    Double height = (double)bitmap.getHeight();
    Double scalingFactor = (960.0/height);
    int tempWidht = bitmap.getWidth();
    Double Dwidth = (tempWidht*scalingFactor);
    int width = Dwidth.intValue();
    Log.v("bitmap dimensions: ", String.valueOf(height) + " + " +String.valueOf(width) + " + " + String.valueOf(scalingFactor));
    bitmap = Utilities.scaleBitmap(bitmap, width, 960);

我用来缩小位图的代码摘录。 它将hight设置为960并获得缩放比例以相应地更改宽度。

编辑:

ScaleBitmap方法。

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
    Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Matrix m = new Matrix();
    m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
    canvas.drawBitmap(bitmap, m, new Paint());
    return output;
}

回复晚了非常抱歉

尝试使用像我在我的应用程序中使用过的whtsapp这样的图像压缩https://www.built.io/blog/2013/03/improving-image-compression-what-weve-learned-from-whatsapp/

您可以使用Picasso库。 这里得到它。 语法如下所示:

Picasso.with(getContext()).load(imagePath).into(imageView);

您可以根据需要尝试使用此代码来调整图像大小

public Bitmap decodeSampledBitmapFromResource(String Filepath,
                                                  int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(Filepath, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(Filepath, options);
}

public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {

    // Raw height and width of image
    int inSampleSize = 1;
    final int height = options.outHeight;
    final int width = options.outWidth;


    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

在这里检查http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM