简体   繁体   中英

Android Studio Pass Image from Gallery as Intent Result

I have three activites, the first one gets text and passes it to the second, the second activity gets another text and if pressed a back button returns the text to the first activity (so I can save the text if a return from the first to the second activity), else if pressed a next button passes all the texts (from first and second activity) to the third activity.

Now my problem is on the third activity: I need to get an image from the phone gallery / take a picture with the camera, and do the same thing as the second: pass it to the second activity if pressed the back button or pass it to a final activity (with all the texts passed earlier) pressing a next button. I can't get to do that. Do I have to return the URI or the Bitmap file?

The code I'm using is this. These are the methods used for the gallery and for taking the photo, I'm using them on two different buttons:

private void openGallery()
    {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

This is the onActivityResult, I'm trying to save the URI to pass it to the previous activity (with no results).

 @Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE)
    {
        imageUri = data.getData();
        imageView.setImageURI(imageUri);

    }
    else
    {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        savedImage = bitmap;
        imageView.setImageBitmap(bitmap);
    }
}

This is the back button

backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (imageUri != null)
                {
                    Intent data = new Intent();
                    data.setData(imageUri);
                    setResult(RESULT_OK, data);
                }
                finish();
            }
        });

And then this on the previous activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == requestCode)
    {
        if (resultCode == RESULT_OK)
        {
            imageUri = data.getData();

        }
    }
}

Try this (Assuming you already have the image from Gallery or Camera)

1). Convert Bitmap to Byte Array:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

2). Pass byte array into intent:-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

3). Get Byte Array from Bundle and Convert into Bitmap Image:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

or

1).First Activity

Uri uri = data.getData();
        Intent intent=new Intent(Firstclass.class,secondclass.class);
        intent.putExtra("imageUri", uri.toString());
        startActivity(intent);

2).Second class

Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
Bundle extras = getIntent().getExtras();
myUri = Uri.parse(extras.getString("imageUri"));
iv_photo.setImageURI(myUri);

You can pass the uri to other activity simply

intent.putExtra("imageUri", imageUri.toString());

and then receive it in the next activity as

Bundle extras = getIntent().getExtras();
Uri myUri = Uri.parse(extras.getString("imageUri"));

Hope this helps.

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