-1

OK, so here is what I want to do in this android activity:

  1. Press the button that says "choose pic"
  2. Use intent or whatever, and go to choose a pic from your local photo library
  3. Once you have chosen the pic, go to this activity
  4. And this time, the image view below would be set (it will be the picture you've chosen)

enter image description here

I have read this q&a and tried the code below, in the activity, but it didnt work out. android pick images from gallery *for the last line I couldn't figure out what to write, I just wanted to set the exact picture

public void choosepic (View v){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");

    Intent pickIntent = new Intent(Intent.ACTION_PICK,         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    pickIntent.setType("image/*");

    Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

    startActivityForResult(chooserIntent, PICK_IMAGE);
    foodpic.setpic
}
leecat
  • 1
  • 1

1 Answers1

0

As your code says image cannot be set from the method where you launch the intent. Override method with name onActivityResult() and use the following code in that method

 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null) {
                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();

                // String picturePath contains the path of selected Image
ImageView imageView = (ImageView) findViewById(R.id.imgView);

            Bitmap bmp = null;
            try {
                bmp = getBitmapFromUri(selectedImage);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            imageView.setImageBitmap(bmp);
            }

Hope this helps.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • excuse me, I have no knowledge about doing a protected void onactivity result for a button. Do you know where in the code that mentions about the button? – leecat Aug 17 '19 at 06:58
  • If possible, do you know how to do the same thing with the oncreatelistener method? – leecat Aug 17 '19 at 07:24
  • onActivityResult is not specific for a button just try the above code it should work I have also edited the answer and have added snippet of displaying an image also. – keshav kowshik Aug 18 '19 at 17:25