简体   繁体   中英

Android: Choose Photo from Gallery

I am trying to access photo libraray in android device using this code:

txtSelectPhoto.setOnClickListener(v->{
        Intent i = new Intent(Intent.ACTION_PICK);
        i.setType("image/*");
        startActivityForResult(i,GALLERY_CHANGE_PROFILE);
    });

but when it opens it just gives me a blank screen of the device folders with no photos like this although there are photos on the device.. is there any way to solve this?

在此处输入图像描述

Remove setType() and instead pass a Uri in the Intent constructor, representing the collection that you want the user to pick from:

txtSelectPhoto.setOnClickListener(v->{
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, GALLERY_CHANGE_PROFILE);
});

Or, keep the MIME type and switch to ACTION_GET_CONTENT :

txtSelectPhoto.setOnClickListener(v->{
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("image/*");
    startActivityForResult(i, GALLERY_CHANGE_PROFILE);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM