1

I am looking for a way to open onClick the gallery to pick an Picture. I want to use that local "uploaded" picture in a listview of pictures. Can someone help and explain me how to do that. I am a beginner in Android Developing.

public void pickImage() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            //Display an error
            return;
        }
        InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
        //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
    }
}
TonyG
  • 1,432
  • 12
  • 31
Supito
  • 23
  • 4
  • You can find a hundred and more examples on the internet and on stackoverflow. – greenapps Oct 18 '17 at 12:47
  • Yes thats right and I tried hundred of them :D Right now I am trying this but I get "Cannot find symbol context" – Supito Oct 18 '17 at 12:50
  • 1
    Possible duplicate of [android pick images from gallery](https://stackoverflow.com/questions/5309190/android-pick-images-from-gallery) – Vishal Yadav Oct 18 '17 at 12:50
  • I edited my question for showing u my code – Supito Oct 18 '17 at 12:51
  • From where do you get `context` variable in `onActivityResult` function? Replace `context` with `YourActivityName.this`. Or if you have this code in fragment use `getActivity()` function instead. – BVantur Oct 18 '17 at 12:55
  • And when i delete the line with context i get mSecurityInputMethodService is null – Supito Oct 18 '17 at 12:55
  • So how should the line looks like? – Supito Oct 18 '17 at 12:57
  • `InputStream inputStream = YourActivityName.this.getContentResolver().openInputStream(data.getData());` YourActivityName is name of your Activity. – BVantur Oct 18 '17 at 12:58
  • Thanks but now I have the same problem with "data" – Supito Oct 18 '17 at 13:00

1 Answers1

0

Here is a code i used in my app

Intent pickImageIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickImageIntent, PICKIMAGE_REQUESTCODE);

And at the onActivity result method

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PICKIMAGE_REQUESTCODE) {
                if (resultCode == RESULT_OK) {
                    Uri imageUri = data.getData();
                    InputStream inputStream = null;


                    try {
                        inputStream = getContentResolver().openInputStream(imageUri);
                       bitmap = BitmapFactory.decodeStream(inputStream);
                    } 
                   catch (FileNotFoundException e) 
                     {
                        e.printStackTrace();
                    } finally {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.d(TAG, "onActivityResult: error closing InputStream during reading image from the phone external storage");
                        }

                    }
    }
Yirga
  • 881
  • 1
  • 12
  • 31