0

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;
}'
  • actually what you need is real path, what you have right now is content path try this https://stackoverflow.com/a/41520090/19491703 – Beant Singh Jul 09 '22 at 06:49
  • No you do not need or can use an absolute path or a real path. Just take the uri and then use ImageView.loadUri(uri) or something like that. – blackapps Jul 09 '22 at 07:09

1 Answers1

0

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;
   
    }
}
Karamat Subhani
  • 104
  • 1
  • 7