简体   繁体   中英

How to load images from phone storage in android studio

I have been trying from long time to solve the problem. I'm beginner in android studio programming language "Java". I'm trying to load images from phone storage into my application but the problem is when I click on ImageView it shows the image path which I set it already onClick, but it not showing the image in image view what should I do. Do you have any source code of gallery application. Below I shared my code. Please help me if you have proper knowledge I'll be thankful. And sorry for my English.

' public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder>{

private Context mContext;
private List<String> images;
private PhotoListener photoListener;

public GalleryAdapter(Context mContext, List<String> images, PhotoListener photoListener) {
    this.mContext = mContext;
    this.images = images;
    this.photoListener = photoListener;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new MyViewHolder(
            LayoutInflater.from(mContext).inflate(R.layout.gallery_item , parent , false)
    );
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    String image = images.get(position);
    Glide.with(mContext).load(image).into(holder.imageView);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            photoListener.onPhotoClick(image);
        }
    });
}

@Override
public int getItemCount() {
    return images.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{

    ImageView imageView;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.ImageView);
    }
}

public interface PhotoListener{
    void onPhotoClick(String path);
}

}

public class ImageGallery { public static ArrayList listOfImages(Context context){

    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String [] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
    String orderBy = MediaStore.Images.Media.DATE_MODIFIED;
    Cursor cursor = context.getContentResolver().query(uri , projection, null , null , orderBy + " DESC");
    String absolutePathOfImage;
    ArrayList<String> ImagesList = new ArrayList<>();
    int columnIndexData = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    while (cursor.moveToNext()){
        absolutePathOfImage = cursor.getString(columnIndexData);
        ImagesList.add(absolutePathOfImage);
    }
    return ImagesList;
}'

You may use a photo picker. a photo picker is a tool where you can get images of your phone. you may select a single image or multiple images using photo picker For now i will explain how you will get single image

when you press on click

@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_PICK_IMAGES);
startActivityForResult(intent, PHOTO_PICKER_REQUEST_CODE);
}

you may select any image from the Photo picker. on selecting any images, onActivityResult() handles callbacks from the photo picker.

// onActivityResult() handles callbacks from the photo picker.
@Override
protected void onActivityResult(
    int requestCode, int resultCode, final Intent data) {

    if (resultCode != Activity.RESULT_OK) {
        // Handle error
        return;
    }

    switch(requestCode) {
        case REQUEST_PHOTO_PICKER_SINGLE_SELECT:
            // Get photo picker response for single select.
            Uri currentUri = data.getData();

            //you got image path, now you may use this 
            return;
   
    }
}

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