简体   繁体   English

如何从图库中调整图像大小

[英]how to resize image from gallery

I want to select image from gallery and send it to Second activity but image is too big . 我想从图库中选择图像并将其发送到Second activity,但是图像太大。

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 . Passing bitmaps从一种活动Passing bitmaps到另一种活动是有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. 最好的方法是只pass Uri而不是将位图从第一个活动传递到第二个活动,然后在需要时将Uri转换为位图。

First Collect the URI 首先收集URI

Uri uri = data.getData();  

and pass the URI from One Activity to Other 并将URI从一个活动传递给另一个

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* * 并在Showdata_result_resume活动中获取URI *

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

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

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