簡體   English   中英

調整從圖庫中選取的圖像

[英]resize image picked from gallery

我正在使用一個應用程序,它從圖庫中獲取圖像並將其保存到解析中。

問題是,當我選擇相機拍攝的圖像時,圖像的尺寸太大,需要幾秒鍾才能在圖像視圖中加載圖像。 此外,當我保存圖像並在應用程序中再次下載時,它需要花費很多時間,因為它必須下載一個大圖像。

我不知道如何縮小所拾取圖像的大小。 我嘗試了幾件事但是有效。

這是我此時的代碼

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();




        }
    });


}

有誰知道怎么做?

謝謝你的幫助,

這個答案對你有幫助

如果您希望位圖比率相同並減少位圖大小。 然后傳遞你的最大尺寸位圖。 你可以使用這個功能

 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); } 

SELECT_PHOTO更改你的onActivityResult ,如下所示:

   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();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM