last day (15 days later) » 

6:59 AM
0
Q: ConcurrentModificationException-ArrayList

HarshitaI have been through the documentation and multiple related posts, but am unable to detect where and why the exception is occurring in my code. I'm trying to click an image, display it in a recyclerview (for which I'm storing the path in an ArrayList) and upload them. I have two ArrayLists- one fo...

 
can you post the error logs as well
 
Yes. Give me a minute
 
looks as your operations are not thread safe. Are you calling a method from different threads? Also the captureImage method, where exactly are you calling it. Also your code is not memory friendly. you're creating too many bitmaps. One better way to do this is to create a new activity and add a fab which opens it. Capture an image from the new activity and then push just the URI of the image to the recyclerview.
 
I'm calling a method from a different method. The captureImage is called on button.OnClickListener.I know the code is not memory friendly. I'll work on it as soon as I'm done with the exception.
 
where is the onClickListener method written? inside the recyclerview adapter?
 
6:59 AM
Wait, I'll update the code snippet.
 
lets continue here
 
YEAH, hI
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
captureImage();
}
catch (Exception exception){
Toast.makeText(getApplicationContext(),"Please wait"+exception.toString(),Toast.LENGTH_LONG).show();
}

}
});
And I've already posted the captureImage() function
 
yes but where exactly is this code? inside you activity or inside your adapter
 
The activity
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.image_result.setImageURI(Uri.parse(horizontalList.get(position))); // setting image in the recycler view
holder.image_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),Image_Preview.class);
intent.putExtra("abc",horizontalList.get(position));
v.getContext().startActivity(intent);
}
});
}
This is the method used to display image in recycler view
 
ok. please make me understand a few things.
1. is the captureImageMethod hosteed in the activty or adapter
if the activty which activity. one which is opened via the adapter
?
 
7:04 AM
Okay. I'll explain
There is a dashboard activity. When I click camera, The Camera activity opens up.
Now wait, I'll show you
I'm sharing the image. One min
The 3 images on the bottom are basically displayed in recyclerView
When I click the orange button, the listener is set in onCreate method, and capturImage is called in onClick function
 
7:24 AM
Should I expect a reply?
 
sorry was out for lunch
 
Okay
 
why are you creating an iterator
??
 
7:47 AM
Its not being used
I was just looking for solution, toh just like that
 
7:59 AM
Should I wait? Please tell me. I'm stuck
 
8:17 AM
found the problem
you're setting this interface everytime you're calling the method
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
instrad, set it once
and create a method captureImage
put all the code inside the onPictureTaken method implementation and put it in this method
also make sure your operations happen inside a worker thread
try this it should work.
 
okay okay. Don't go Let me do it.
i'm coming back.
Please don't go
 
8:37 AM
captureImage is a library functiontoo
 
what?
you said you've written the code inside an onclick implementation
 
8:50 AM
No wait
Yes.
Not this. Oh god.
I'll try
 
one sec
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
upload_bitmap= BitmapFactory.decodeByteArray(picture, 0, picture.length);
result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
captureImage();
}
});
}
public void captureImage(){
large_bitmap=getResizedBitmap(upload_bitmap,500);
bytes = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
Bitmap newResult=getResizedBitmap(result,400,400);
String fileNameSmall = new SimpleDateFormat("yyyyMMddHHmmss'.txt'").format(new Date());
String fileNameLarge= new SimpleDateFormat("HHmmss'.txt'").format(new Date());
path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), newResult, fileNameSmall, null);
same error aa raha
 
9:11 AM
yes because you're resetting the interface on every click
look at this
`button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
upload_bitmap= BitmapFactory.decodeByteArray(picture, 0, picture.length);
result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
captureImage();
}
});
}`
so on every click. a new camera listener is set. that means the previous one is also living and both of them are trying to modify the data
 
What should I do?
The documentation of this library I used explained this only
 
why are you implementing an onclick interface
your library provide it right
inside your onCliclk add this line
cameera.captureImage();
and move the camera listener out side
 
9:26 AM
okay
i did
horizontal_rv.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState){
super.onScrollStateChanged(recyclerView, newState);
}

});
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
upload_bitmap= BitmapFactory.decodeByteArray(picture, 0, picture.length);
result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
captureImage();
camera listener outside
@Override
public void onClick(View view) {
try{
camera.captureImage();
captureImage();
}
catch (Exception exception){
Toast.makeText(getApplicationContext(),"Please wait",Toast.LENGTH_SHORT).show();
}

}
});
But there's an exception
 
not this line
captureImage();
this stays in your cameralistener. get your basic correct. I'll have to leave. That solution will work. it worked for me. if it works. pls do mark it as an answer
 
Leaving now?
 
check the answer
 
yes
Now, when I click the image, it is not displayed simultaneously
The previous image is displayed
I'll mark your answer
 

last day (15 days later) »