I am trying to attach files to the letter. Now it turns out to choose a file from the explorer and send it to the array. Next, I display the attached files in the list. What is the problem - I can attach two identical files, and sometimes more 5. I can not understand what the problem is. In the end, I did a check, but it does not work as it should. Here are my steps to attach a file:

  1. Select file via adapter:

    fileManager.itemView.setOnClickListener(view -> { if (file.isDirectory()) { ((WriteResponseMess) ctx).recreateRecyclerView(file.getPath()); } else { if (getFolderSize(file) > 20) { ((WriteResponseMess) ctx).convertFileToString(file.getPath()); } else { Log.i("m", ">20"); } } }); 

    In the adapter, I check if the selected item in the list is a file or folder, and if this is a file, then we proceed to step 2.

  2. Encode the file:

     public void convertFileToString(String pathOnSdCard) { dialog.dismiss(); File file = new File(pathOnSdCard); if (ms.getArray() != null) { if (ms.getArray().size() > 0) { for (int i = 0; i < ms.getArray().size(); i++) { JsonObject object = ms.getArray().get(i).getAsJsonObject(); String nFile = object.get("filename").getAsString(); Log.i("m", nFile + "\n" + object.get("filename")); if (!file.getName().equals(nFile)) { try { byte[] data = FileUtils.readFileToByteArray(file); uploadFiles(ms.getArray(), new File(pathOnSdCard).getName(), Base64.encodeToString(data, Base64.NO_WRAP)); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(this, "You have already attached this file", Toast.LENGTH_SHORT).show(); } } } else { try { byte[] data = FileUtils.readFileToByteArray(file);//Convert any file, image or video into byte array uploadFiles(ms.getArray(), new File(pathOnSdCard).getName(), Base64.encodeToString(data, Base64.NO_WRAP)); } catch (IOException e) { e.printStackTrace(); } } } } 

In this function, I check if there are already elements in the array, if there is, then I check if there is such a file already in the array, and it is this test that for some reason does not work for me correctly. It turns out if I choose the same file a second time, then I get a toast that the file has already been selected once, and anyway the file is attached. I feel that the problem is somewhere in the cycle but could not find where exactly. I hope for your help :)

    1 answer 1

    In the loop, you only need to check if there is an element in the array. And add after the cycle, if the duplicate is not found.

     public void convertFileToString(String pathOnSdCard) { dialog.dismiss(); File file = new File(pathOnSdCard); JsonArray array = ms.getArray(); if (array == null) { return; } for (int i = 0; i < array.size(); i++) { JsonObject object = array.get(i).getAsJsonObject(); String nFile = object.get("filename").getAsString(); Log.i("m", nFile + "\n" + object.get("filename")); if (file.getName().equals(nFile)) { Toast.makeText(this, "You have already attached this file", Toast.LENGTH_SHORT).show(); return; } } try { byte[] data = FileUtils.readFileToByteArray(file);//Convert any file, image or video into byte array uploadFiles(array, file.getName(), Base64.encodeToString(data, Base64.NO_WRAP)); } catch (IOException e) { e.printStackTrace(); } } 
    • there is a small problem with the solution you suggested, here is the situation - they attached one file, everything is fine, they attached the second file, everything is fine, we attach the third file - 2 files are attached in the end, we attach the fourth file - 4 files are attached, something is wrong probably works :( - Andrew Goroshko
    • In this piece of code, I do not see any problems. It works as it was intended: if there is no file, then the last block will be executed, if there is, it will show a toast and exit the method. Look for the cause further. - woesss
    • You gave me the idea of ​​how to improve your method of loading into an array and it seemed to work, thank you very much for your help :) - Andrew Goroshko