Kane Smith Sillapasit
Kane Smith Sillapasit

Reputation: 29

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

Upvotes: 1

Views: 189

Answers (3)

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

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.

Upvotes: 1

Linga
Linga

Reputation: 10553

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

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 8939

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

Upvotes: 0

Related Questions