调整从图库中选取的图像 [英] resize image picked from gallery

查看:93
本文介绍了调整从图库中选取的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序,它从图库中获取图像并将其保存到解析中。

I'm working in an app that take an image from the gallery and save it into parse.

问题是,当我选择由相机的图像尺寸太大,需要几秒钟才能在图像视图中加载图像。此外,当我保存图像并在应用程序中再次下载时,它需要花费很多时间,因为它必须下载一个大图像。

The problem is that when I pick an image taken by the camera the size of the image is too big and takes some seconds to load the image in an image view. Also when I save the image and download again in the app it takes a lot of time because it has to download a big image.

我不知道如何减少拾取图像的大小。我尝试了几件事,但有效的。

I don't know how I reduce the size of the image picked. I tried several things but anything works.

这是我此时的代码

public class Datos extends Activity implements OnItemSelectedListener {


private final int SELECT_PHOTO = 1;
private ImageButton imageView;


ParseFile file;
byte [] data;

/*
    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
    }
*/


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_datos);
    btnClick();

    imageView = (ImageButton) findViewById(R.id.imageButton);


    ImageButton pickImage = (ImageButton) findViewById(R.id.imageButton);
    pickImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            //photoPickerIntent.putExtra("outputX", 150);
            //photoPickerIntent.putExtra("outputY", 100);
            //photoPickerIntent.putExtra("aspectX", 1);
            //photoPickerIntent.putExtra("aspectY", 1);
            //photoPickerIntent.putExtra("scale", true);
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });

}

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


    switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                try {
                    Uri imageUri = imageReturnedIntent.getData();
                    InputStream imageStream = getContentResolver().openInputStream(imageUri);
                    Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    imageView.setImageBitmap(selectedImage);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    selectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);

                    data = stream.toByteArray();

                    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), selectedImage);
                    imageView.setBackgroundDrawable(bitmapDrawable);

                    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
                    Bitmap bmp = drawable.getBitmap();
                    Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false);


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }


            }

    }
}



@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
                           long id) {
    // TODO Auto-generated method stub

    Spinner spinner = (Spinner) parent;
    if (spinner.getId() == R.id.telefono) {
        consola = parent.getItemAtPosition(position).toString();
    } else if (spinner.getId() == R.id.provincia) {
        provincia = parent.getItemAtPosition(position).toString();
    }

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}


public void btnClick() {

    Button buttonEnviar = (Button) findViewById(R.id.enviar);


    buttonEnviar.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {

            //Storing image in parse passed in  onclick method of a button with the below code:


            Intent intentDatos = new Intent(Datos.this, Inicio.class);
            startActivity(intentDatos);


            ParseObject testObject = new ParseObject("Musica");

            if (data != null) {

                file = new ParseFile("selected.png", data);
            }

            else {

                data = "".getBytes();
                file = new ParseFile("selected.png", data);
            }

            //file.saveInBackground();


            testObject.put("imagen", file);

            testObject.saveInBackground();




        }
    });


}

有人知道怎么做吗?

感谢您的帮助,

推荐答案

这个答案将帮助您


如果你想要位图比率相同并减少位图大小。然后传递
最大大小的位图。你可以使用这个函数

if you want bitmap ratio same and reduce bitmap size. then pass your maximum size bitmap. you can use this function

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
}


更改 onActivityResult in SELECT_PHOTO 这样的情况:

change your onActivityResult in SELECT_PHOTO case like this:

   case SELECT_PHOTO:
        if (resultCode == RESULT_OK) {
            try {
                Uri imageUri = imageReturnedIntent.getData();
                InputStream imageStream = getContentResolver().openInputStream(imageUri);
                Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

                selectedImage = getResizedBitmap(selectedImage, 400);// 400 is for example, replace with desired size

                imageView.setImageBitmap(selectedImage);


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

这篇关于调整从图库中选取的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆