mrl25
mrl25

Reputation: 139

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

}

Upvotes: 14

Views: 14799

Answers (3)

Bigflow
Bigflow

Reputation: 3666

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.

Upvotes: 5

FoamyGuy
FoamyGuy

Reputation: 46856

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

Upvotes: 2

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9791

Try to use

.... 
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, SELECT_PICTURE);
....

Upvotes: 28

Related Questions