Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get only image from gallery

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;
        }  
      }  

}

like image 586
mrl25 Avatar asked Mar 06 '12 14:03

mrl25


2 Answers

Try to use

.... 
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, SELECT_PICTURE);
....
like image 62
Vyacheslav Shylkin Avatar answered Sep 28 '22 04:09

Vyacheslav Shylkin


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);
like image 39
FoamyGuy Avatar answered Sep 28 '22 03:09

FoamyGuy