简体   繁体   中英

how to resize image from gallery

I want to select image from gallery and send it to Second activity but image is too big .

I need to resize it and I dont know how to do it:

buttonIntent.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {               
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_GALLERY);

method onresult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {

                Uri uri = data.getData();  
                try {
                    bitmap = Media.getBitmap(this.getContentResolver(), uri);
                    imageView1.setImageBitmap(bitmap);

    } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
btnok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i=new Intent(Showpic_resumeActivity.this,Showdata_result_resume.class);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
            i.putExtra("byteArray", bs.toByteArray());
            startActivity(i);

        }
    });

second activity

if (getIntent().hasExtra("byteArray")) {

        Bitmap b = BitmapFactory.decodeByteArray(
                getIntent().getByteArrayExtra("byteArray"), 0, getIntent()
                        .getByteArrayExtra("byteArray").length);
        image_resume.setImageBitmap(b);
    }

Passing bitmaps from one activity to another activity is risk . Best way is just pass Uri instead of passing bitmaps from first activity to second and then convert Uri to bitmap when it requires.

First Collect the URI

Uri uri = data.getData();  

and pass the URI from One Activity to Other

btnok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i=new Intent(Showpic_resumeActivity.this,Showdata_result_resume.class);
            i.setData(uri );// Passing the URI 
            startActivity(i);

        }
    });

* And get Back the URI in Showdata_result_resume Activity*

Uri uri=getIntent().getData();
YOUR_IMAGVIEW.setImageURI(uri);// Set URI to your ImageView

Try this code:

Bitmap bmp=BitmapFactory.decodeResource(getResources(), your_image_loc);//ex:R.drawable.image1
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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