2
votes

As the title pretty much states.

here is what i am trying to do

1) Open Gallery (I know how to do this) 2) Choose image from gallery 3) Get the Image Id and pass the ID to another Activity 4) In this other activity i want to load this image into an ImageView.

I know i can do this instead

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

then pass array to intent

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

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

But i believe that it will be better on memory and performance by passing ID instead and then getting id in the next activity and loading image from the SD card.

1

1 Answers

0
votes

I have a few code snippets from a project I was working on a while back. Maybe this will help: (Sorry if some of the code is irrelevant, as I said from an older project of mine)

First and foremost of course, find the picture

 Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);

When the user chooses a picture, onActivityResult will be called and bring the user back to the prior Activity (which started it most of the time). Here, we can get some data that we can pass onto the next Activity, like image URI for example.

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

        switch (requestCode) {

        case SELECT_PICTURE:
            // Uri selectedImageUri;
            selectedImageUri = data.getData();

            if (pic.getDrawingCache() != null) {
                pic.destroyDrawingCache();
            }

            pic.setImageURI(selectedImageUri);
            pic.buildDrawingCache(true);
            pic.setDrawingCacheQuality(ImageView.DRAWING_CACHE_QUALITY_HIGH);
            pic.setMaxHeight(pic.getDrawable().getBounds().width());

            tvPicInfo
                    .setText(String.valueOf(pic.getDrawable().getBounds()
                            .width())
                            + "px"
                            + " x "
                            + String.valueOf(pic.getDrawable().getBounds()
                                    .height()) + "px");

            break;

        case 2:

            selectedImageUri = data.getData();

            if (pic.getDrawingCache() != null) {
                pic.destroyDrawingCache();
            }

            pic.setImageURI(selectedImageUri);
            pic.buildDrawingCache(true);
            pic.setDrawingCacheQuality(ImageView.DRAWING_CACHE_QUALITY_HIGH);
            break;

        }

    }
}

Now that we have the chosen image's uri, this method can get it's path, which you can pass to next activity in bundle.

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

In the next Activity, load path from bundle like you are for the byte array. With that path, decode with BitmapFactory

Bitmap bitmap = BitmapFactory.decodeFile(path);

I hope this helps you on your journey, Happy Coding!

EDIT: Else { how to send imageview from one activity to other } even though this seems pretty much what you don't want