ConcurrentModificationException的-的ArrayList

时间:2017-10-12 06:07:14

标签: android exception bitmap

我已浏览过文档和多篇相关帖子,但无法检测代码中发生异常的位置和原因。我正在尝试单击图像,将其显示在recyclerview中(我将其存储在ArrayList中)并上传它们。我有两个ArrayLists-一个用于在recyclelerView中显示的较小位图,另一个用于要上载的较大位图。

这是捕获图像的代码:

           public void captureImage(){
    camera.setCameraListener(new CameraListener() {
        @Override
        public void onPictureTaken(byte[] picture) {
            super.onPictureTaken(picture);
            upload_bitmap= BitmapFactory.decodeByteArray(picture, 0, picture.length);
            large_bitmap=getResizedBitmap(upload_bitmap,500);
            result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
            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);
            large_bitmap_path= MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), large_bitmap, fileNameLarge, null);
            c = Calendar.getInstance();
            df = new SimpleDateFormat("yyyy-MM-dd");
            tf=new SimpleDateFormat("hh.mm.ss  a");
            formattedTime=tf.format(c.getTime());
            formattedDate = df.format(c.getTime());
            fileName="999999_".concat(formattedDate.toString()).concat(" at ").concat(formattedTime.toString()).concat(".jpg");
            String uploadName=fileName;
            Log.e("fileName",uploadName);
            fileNameList.add(fileName);
            horizontalList.add(path);
            large_bitmapList.add(Uri.parse(large_bitmap_path));
            i++;
            iterator = horizontalList.iterator();
            recycleViewAdapter.notifyDataSetChanged();

        }
    });
    camera.captureImage();

}

这是上传图片的代码:

      final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //Disimissing the progress dialog
                    loading.dismiss();
                    //Showing toast message of the response
                    Toast.makeText(CameraActivity.this, s , Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    //Dismissing the progress dialog
                    loading.dismiss();
                    //Showing toast
                    Toast.makeText(CameraActivity.this, "error", Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
         String img_path=image_path;
            //Converting Bitmap to String

            //Creating parameters
            Map<String,String> parameters = new Hashtable<String, String>();
            //Adding parameters
            parameters.put("dateTime", "2017-10-09 01.43 PM");
            parameters.put("type", "image");
            parameters.put("fileName",fileNameList.get(j));
            j++;
            //returning parameters
            return parameters;
        }
    };

    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
} 

显然我不确定异常发生在哪里。工作有时但有时候,一打开相机就会崩溃。 此外,存储两个不同大小的图像并不是一个好主意,欢迎任何建议。 请尝试帮助而不是downvoting。谢谢。

Logcat

  

java.util.ConcurrentModificationException                                                                                            at java.util.LinkedHashMap $ LinkedHashIterator.nextEntry(LinkedHashMap.java:346)                                                                                            at java.util.LinkedHashMap $ KeyIterator.next(LinkedHashMap.java:366)                                                                                            在android.hardware.Camera $ Parameters.flatten(Camera.java:4243)                                                                                            在android.hardware.Camera.setParameters(Camera.java:3215)                                                                                            在com.flurgle.camerakit.Camera1.setFlash(Camera1.java:139)                                                                                            在com.flurgle.camerakit.Camera1.adjustCameraParameters(Camera1.java:376)                                                                                            在com.flurgle.camerakit.Camera1.openCamera(Camera1.java:317)                                                                                            在com.flurgle.camerakit.Camera1.start(Camera1.java:85)                                                                                            在com.flurgle.camerakit.CameraView $ 3.run(CameraView.java:216)                                                                                            在java.lang.Thread.run(Thread.java:818)

PS Camerakit是我用来创建自定义适配器的库

1 个答案:

答案 0 :(得分:0)

The problem that's happening here is that you're trying to set a new cameraListener on every onClick event of your button.

CameraKit library has a function, camera.captureImage() use this method inside your onClick implementation. M

Move the cameraListener out of the onClick implementation.

The exception clearly states that you're trying to modify a value concurrently. Possibly because you're adding a new listener everytime.

So you final code should be something like this:

1. Your onclick:

@Override 
public void onClick(View view) { 
try{ 
camera.captureImage();  
} 
catch (Exception exception){ 
Toast.makeText(getApplicationContext(),"Please wait",Toast.LENGTH_SHORT).show(); 
} 

} 
});

2. Your listener:

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(); 
} 
}); 

3. Your captureImage(Bitmap bitmap):

Save the bitmap and return a uri