Issue in Sending Multiple Images to the Server in Android

31 views Asked by At

This method takes picture and adds it in the list but the issue is OutOfMemory Exceptions and all that. I have to send the images to the Server through API. But the pictures are never reaching there. Whenever I close my application the server says the request was in progress. I can't identify the issue.

imageCapture.takePicture(getExecutor(), new ImageCapture.OnImageCapturedCallback() {
                @Override
                public void onCaptureSuccess(@NonNull ImageProxy image) {
                    super.onCaptureSuccess(image);

                    tempheight = image.getHeight();
                    tempwidth = image.getWidth();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();

                    byte[] imageData = new byte[buffer.remaining()];
                    buffer.get(imageData);
                    image.close();
                    buffer.clear();

                    // Create a RequestBody from the captured image data



                    RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), imageData);
                    // Prepare a MultipartBody.Part using the RequestBody
                    MultipartBody.Part imagePart = MultipartBody.Part.createFormData("img", "image.jpeg", requestFile);

                    //WebApi api = RetrofitClient.getInstance().getMyApi();

                    collectingImages.add(imagePart);

                }


                @Override
                public void onError(@NonNull ImageCaptureException exception) {
                    super.onError(exception);
                }
            });
public void imageSendingForNavigtion(ArrayList<MultipartBody.Part> images, RequestBody height, RequestBody width, RequestBody floor) {
        //Toast.makeText(this, "In image sending for navigation method", Toast.LENGTH_SHORT).show();


        if (isApiRequestInProgress) {

            Toast.makeText(this, "Another Api request in Process", Toast.LENGTH_SHORT).show();
            return; // An API request is already in progress, skip capturing this time
        }
        isApiRequestInProgress = true;


            try {

                Toast.makeText(this, images.size()+"", Toast.LENGTH_SHORT).show();
                WebApi api = RetrofitClient.getInstance().getMyApi();
                api.frame(images, height, width, floor).enqueue(new Callback<ApiResponseOfFrame>() {
                    @Override
                    public void onResponse(Call<ApiResponseOfFrame> call, Response<ApiResponseOfFrame> response) {
                        if (response.isSuccessful()) {
                            ApiResponseOfFrame s=response.body();
                            which_objects = s.getCoordinates();

                            ArrayList<Coordinate> screenCoordinates = new ArrayList<>();

                            // Calculate the scaling factors for X and Y coordinates
                            float scaleX = (float) overlayView.getWidth() / tempwidth; // Adjust for screen width
                            float scaleY = (float) overlayView.getHeight() / tempheight;
                            for (Coordinate apiCoordinate : which_objects) {
                                // Map API coordinates to screen coordinates
                                int left = (int) (apiCoordinate.getXmin() * scaleX);
                                int top = (int) (apiCoordinate.getYmin() * scaleY);
                                int right = (int) (apiCoordinate.getXmax() * scaleX);
                                int bottom = (int) (apiCoordinate.getYmax() * scaleY);

                                Coordinate coordinate = new Coordinate();
                                coordinate.setXmax(right);
                                coordinate.setXmin(left);
                                coordinate.setYmax(top);
                                coordinate.setYmin(bottom);
                                coordinate.setDistance(apiCoordinate.getDistance());
                                coordinate.setLabel(apiCoordinate.getLabel());
                                coordinate.setSteps(apiCoordinate.getSteps());
                                coordinate.setPosition(apiCoordinate.getPosition());
                                screenCoordinates.add(coordinate);


                            }

                            which_objects = screenCoordinates;
                            updateOverlay(which_objects);
                            isApiRequestInProgress = false;
                            counter=counter+1;

                        }
                    }

                    @Override
                    public void onFailure(Call<ApiResponseOfFrame> call, Throwable t) {
                        isApiRequestInProgress = false;
                    }
                });
            } catch (Exception e) {
                Log.e("e", e.getMessage());
            }


    }

I have tried writing that buffer into a file and then using the file as a whole but nothing worked.

0

There are 0 answers