0

i am creating staggered view inside RecyclerView and showing SD card folder images.

What i have done : I managed to create folder on SD card and saving all clicked images there .

iSSUE : Now i am unable to show those images in folder on staggered recycler view .

Recyclerview layout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout_signup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">


        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view_cameragallery"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/view3"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

RecyclerActivity.class

 StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        cameraRecyclerView.setLayoutManager(gridLayoutManager);
         adapter = new CameraAdapter(new ArrayList<String>(),getContext());
        cameraRecyclerView.setAdapter(adapter);

CameraButton Click Handle (Saving Image in SD card Folder):

 @Override
    public void onClick(View v) {

        newpath = new File(Environment.getExternalStorageDirectory(),
                photoPath + "_" + holder.phone + "/");

        // Create the storage directory if it does not exist
        newpath.mkdirs();

        checkinUUID = UUID.randomUUID();


        //media file name
         tempphoto = new File(newpath,checkinUUID + ".jpg");

        /*create file to save image*/
        photoUri = Uri.fromFile(tempphoto);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //set the image file name
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);

        // handle the returned data in onActivityResult
        startActivityForResult(intent,
                Constant.TAKE_CAMERA_PICTURE_REQUEST);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == Constant.TAKE_CAMERA_PICTURE_REQUEST) {

                if (newpath.isDirectory() == false) {
                    newpath.mkdirs();
                }

                if (newpath.isDirectory() == true) {

                    if(newpath.exists())
                    paths = new String[]{tempphoto.getPath()};
                    MediaScannerConnection.scanFile(getActivity(), paths, null, null);
                    listofImagesPath =new ArrayList<String>();
                    listofImagesPath = RetriveCapturedImagePath();
                    if(listofImagesPath!=null){
                        adapter.addApplications(listofImagesPath);

                    }

                }
            }
        }

        super.onActivityResult(requestCode, resultCode, data);

    }

RetriveCapturedImagePath()

 private ArrayList<String> RetriveCapturedImagePath() {
        ArrayList<String> tFileList = new ArrayList<String>();

        if (newpath.exists()) {
            File[] files=newpath.listFiles();
            Arrays.sort(files);

            for(int i=0; i<files.length; i++){
                File file = files[i];
                if(file.isDirectory())
                    continue;
                tFileList.add(file.getPath());
            }
        }
        return tFileList;
    }

Adapterclass:

public class CameraAdapter extends RecyclerView.Adapter<CameraAdapter.ViewHolder> {

    private ArrayList<String> imagesPath;
    private Context context;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_camera_view, parent, false);
        return new ViewHolder(v);
    }

    public CameraAdapter(ArrayList<String> imagesPath, Context context) {
        this.imagesPath = imagesPath;
        this.context = context;

    }

    public void addApplications(ArrayList<String> candidates) {
        this.imagesPath.addAll(candidates);
        this.notifyItemRangeInserted(0, candidates.size() - 1);

    }

    public void clearApplications() {
        int size = this.imagesPath.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                imagesPath.remove(0);
            }

            this.notifyItemRangeRemoved(0, size);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        for (int i=0;i<imagesPath.size();i++){
            holder.imageview.setImageBitmap(BitmapFactory.decodeFile(imagesPath.get(position)));
        }
//        Bitmap bm = BitmapFactory.decodeFile(imagesPath)
    }


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

    public class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageview ;
        public TextView imageText;
        public ViewHolder(View itemView) {
            super(itemView);
           this.imageview = (ImageView)itemView.findViewById(R.id.camera_image_view);
//            this.imageText = (TextView) itemView.findViewById(R.id.camera_grid_text_id);


        }
    }
}

single_item_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/camera_image_view"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="hello"/>



</LinearLayout>

I have with above code having all the images in SD card folder . But not able to show them in Staggered view .

Please help

young_08
  • 1,196
  • 2
  • 13
  • 35
  • Dont you see any pictures at all or whats happening? – Jeffalee May 12 '16 at 08:46
  • @Jeffalee: pictures are saving in folder in SD card (camera button code if you see) . I want that images to be shown in Grid view . This is not happening . – young_08 May 12 '16 at 09:03

1 Answers1

0

The problem seems to be in your RecyclerActivity.class, when setting the Adapter to your RecyclerView you are adding an empty ArrayList. Thats why your RecyclerView is not showing anything.

    adapter = new CameraAdapter(new ArrayList<String>(),getContext());
    cameraRecyclerView.setAdapter(adapter);

You should set the ArrayList of the imagePaths there like this:

    adapter = new CameraAdapter(imagePaths, getContext());
    cameraRecyclerView.setAdapter(adapter);

Then when the data in the ArrayList changes (after filling it with the retrieved data), just call notifyDatasetChanged() on the RecyclerView's Adapter. That should do the trick.

Also, in your onBindViewHolder method you have a unnecesary for-loop:

     for (int i=0;i<imagesPath.size();i++){
         holder.imageview.setImageBitmap(BitmapFactory.decodeFile(imagesPath.get(position)));
     }
Jeffalee
  • 1,080
  • 1
  • 7
  • 15
  • if you see OnActivityResult() method in button click code , i am calling populating adapter similarly . Code is : listofImagesPath =new ArrayList(); listofImagesPath = RetriveCapturedImagePath(); if(listofImagesPath!=null){ adapter.addApplications(listofImagesPath); addapplication is method in Adapter which setting Adapter array list . – young_08 May 12 '16 at 09:55
  • Also , OnBindViewHolder method has for - loop to show all the images in folder . – young_08 May 12 '16 at 09:57
  • Updated my answer. There is no need to call addApplications() on your adapter. when you give the imagePath list to the constructor of the adapter and then later fill that list (in your activity, not in your adapter) calling notfiyDatasetChanged will update your adapter and show your images. – Jeffalee May 12 '16 at 10:01
  • yes but doing all above , still not showing images in recyclerview – young_08 May 12 '16 at 10:46