14

I’m trying to get an image using the built in gallery. It works fine in the emulator and It opens only gallery but on real device it give me multiple chooses one of them is file manager which enable me to choose any type of files even apk files of course the app crash after that I have this code

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}

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


    if (resultCode == RESULT_OK) {  


    switch(requestCode){    

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


          break;
        }  
      }  

}

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
mrl25
  • 139
  • 1
  • 1
  • 6
  • Uninstall the file manager, as it is clearly a piece of junk. That being said, StackOverflow is for programming questions, and this is not a question. – CommonsWare Mar 06 '12 at 14:18
  • 1
    Seems to me like his question is "How do I allow the user to select a photo using the gallery with an intent." – FoamyGuy Mar 06 '12 at 14:45

3 Answers3

28

Try to use

.... 
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, SELECT_PICTURE);
....
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
  • 2
    The stock Android 4.4 Photos app doesn't take this into account. – Sander Versluys May 15 '14 at 08:05
  • The only rigth answeir. I searched for lots of time - my mistake was setting intent.setAction(Intent.ACTION_GET_CONTENT); Lots of androids provide me choice to select from such apps, like google drive, viber photoes, but i could not parse uri. Thanks for your answeir, sure all standart galleries should handle this properly (i checked on 4 phones) – Anton Kizema Dec 17 '15 at 13:55
5
public void ChoosePicture(View v) {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
     {
      if (resultCode == RESULT_OK)
      {
        Uri photoUri = data.getData();
        if (photoUri != null)
        {
        try {
              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
              cursor.moveToFirst();
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              String filePath = cursor.getString(columnIndex);
              cursor.close();
              bMap_image = BitmapFactory.decodeFile(filePath);
              ImageView img = (ImageView) findViewById(R.id.gallery1);
              img.setImageBitmap(bMap_image);


     }catch(Exception e)
      {}
      }
    }// resultCode
    }// case 1
    }// switch, request code
}// public void onActivityResult

mmh, somehow it changed the position of my last few "}".

This code will let you select an image from the gallery and then show it on an imageview.

I use this code on my device, and works like a charm.

Bigflow
  • 3,616
  • 5
  • 29
  • 52
2

Try using this for your intent:

Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

If you are wanting to always use the stock Gallery Application I don' think you need to use an Intent Chooser so you might be able to change your startActivity to this:

startActivityForResult(intent, SELECT_PICTURE);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156