Harshita
Harshita

Reputation: 392

set images in horizontal view in a custom camera layout

PS: This is an update of previously asked question.

I have a custom camera layout where I need the images to be set in a horizontal as and when they are clicked. I tried using recyclerview for this. First, The image is clicked is stored in a bitmap called result.

Then, I store the path of the image in a variable called path. The path variable is passed to the RecycleViewAdapter through bundle.

And then, using the onBindViewHolder, I'm trying to display the image in a horizontal view.

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            camera.setCameraListener(new CameraListener() {
                @Override
                public void onPictureTaken(byte[] picture) {
                    super.onPictureTaken(picture);
                         Bitmap result = 
   BitmapFactory.decodeByteArray(picture, 0, picture.length);

                         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                         result.compress(Bitmap.CompressFormat.JPEG, 25, bytes);

                         String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), result, "Title", null);
                    horizontalList.add(path);
                         recycleViewAdapter = new RecycleViewAdapter(horizontalList);
                         horizontal_rv.setAdapter(recycleViewAdapter);
                         recycleViewAdapter.notifyDataSetChanged();
                    mBundle = new Bundle();
                    CameraActivity.mBundle.putString("abc", path);
                    horizontalList.add(path);
                }
            });
            camera.captureImage();

        }
    });

And then passed to the adapter class.

    public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {
Uri uri;
String path;
private List<Bitmap> horizontalList;
public class MyViewHolder extends RecyclerView.ViewHolder {
    public ImageView image_result;

    public MyViewHolder(View view) {
        super(view);
        image_result = (ImageView) view.findViewById(R.id.image_result);

    }
}

public RecycleViewAdapter(ArrayList<Bitmap> horizontalList) {
    this.horizontalList = horizontalList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recyclerview_item_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
     path= CameraActivity.mBundle.getString("abc");
    uri=Uri.parse(path);
    holder.image_result.setImageURI(uri);


    holder.image_result.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
}


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

Problem: When I click an image, at each click 2 copies are shown in the image. And the previous views are changed showing the new image, doubling the count. Also, the app crashes after max 3 clicks.

A screenshot is: enter image description here

Please help.

Upvotes: 0

Views: 145

Answers (1)

Clement Osei Tano
Clement Osei Tano

Reputation: 832

//I assume you are looping here
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);

                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    result.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

                    String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), result, "Title", null);

                    CameraActivity.mBundle.putString("abc",path);
                    horizontalList.add(result);
                   // finish loop....................
//notify the adapter of data changes
recycleViewAdapter.notifyDataSetChanged();

Creating a new adapter inside the loop means only a single item can be displayed while also being inefficient. Loop and add the elements in the loop and after the loop, notify the adapter of data changes.

Upvotes: 1

Related Questions