0

I want to be able to select an image from the gallery and be able to show it and zoom in / out I tried this code just to select an image

public static final int PICK_IMAGE = 1;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button buttonLoadImage = (Button) findViewById(R.id.button);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                 Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                 startActivityForResult(i, PICK_IMAGE);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

and when i tried to see if it works .. it opened the gallery but when i selected in image .. it didnt appear in the imageview how can i fix it and how to be able to zoom in and out

Anurag Dhadse
  • 1,722
  • 1
  • 13
  • 26

2 Answers2

0

Use this code inside onActivityResult()

    if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap bitmapImage= BitmapFactory.decodeStream(imageStream);
                ImageView imageView = (ImageView) findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmapImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }
Mr_vmh
  • 171
  • 1
  • 9
  • Thank you .. it worked but how can i zoom in and out ??!! –  Jul 10 '20 at 08:42
  • Go through this link https://stackoverflow.com/questions/8399296/how-to-implement-zoom-effect-for-image-view-in-android – Mr_vmh Jul 10 '20 at 08:57
0

In onActivityResult:

  imageView.setImageUri(data.getData());

Thats all.

blackapps
  • 8,011
  • 2
  • 11
  • 25