C
C
Comatu2018-02-14 06:18:32
Android
Comatu, 2018-02-14 06:18:32

Why do duplicate files occur when uploading to AsyncTask?

There is an internal AsyncTask descendant class, with the help of which I load image files for further display in RecyclerView. At the input of execute() I submit a string array of 9 urls. In the doInBackground method in a loop, the files are loaded and an array of their uri is formed, and then in onPostExecute the array is written to the json file.
In some magical way, after the first complete download cycle, the url string array doubles exactly and files are downloaded again, 18 pieces.
How to eliminate such duplication, where is the mistake? I really need help!

public class LoadCoverActivity extends AppCompatActivity {
    String fileNamePath = "filesPath.json";
    String coversFileName = "coverFilesPath.json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_cover);
        String urlsCover = MyJSON.getData(LoadCoverActivity.this, fileNamePath);
        Gson gsonCovers = new Gson();
        Type founderListType = new TypeToken<ArrayList<String>>(){}.getType();
        ArrayList<String> urlsCoverList = gsonCovers.fromJson(urlsCover, founderListType);
        String[] urlsCovers = urlsCoverList.toArray(new String[0]);
        CoverLoader coverLoader = new CoverLoader();
        coverLoader.execute(urlsCovers);

    }

    private class CoverLoader extends AsyncTask<String, Void, ArrayList<String>> {

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected ArrayList<String> doInBackground(String... urlsFiles) {
            ArrayList<String> coversFiles = new ArrayList<>();
            for (int i = 0; i < urlsFiles.length; i++) {
                int d = i + 1;
                String nameForWrite = "bookcover_" + d + ".jpg";
                String urlCover = urlsFiles[i];
                String filePath = downloadFile(urlCover, nameForWrite);
                coversFiles.add(filePath);
            }
            return coversFiles;
        }

        @Override
        protected void onPostExecute(ArrayList<String> coversFiles) {
            String strJs = new Gson().toJson(coversFiles);
            MyJSON.saveData(getApplicationContext(), strJs, coversFileName);
            nextActivity();
        }
    }

    private void nextActivity() {
        Intent intent = new Intent(LoadCoverActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    public ArrayList<String> getFilesPathFromFile(String jsResult) {
        ArrayList<String> urlsImg = new ArrayList<>();

        try {
            JSONArray rootJson = new JSONArray(new JSONTokener(jsResult));
            for (int i = 0; i < rootJson.length(); i++) {
                JSONObject o = rootJson.getJSONObject(i);
                String strTo = (String) o.get("uriString");
                urlsImg.add(strTo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return urlsImg;
    }

    private String downloadFile(String fileURL, String fileName) {
        String rootDir = String.valueOf(getFilesDir());
        try {
            File rootFile = new File(rootDir);
            if (!rootFile.exists()) {
                rootFile.mkdir();
            }
            URL url = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            FileOutputStream f = new FileOutputStream(new File(rootFile,
                    fileName));
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (IOException e) {
            Log.d("Error....", e.toString());
        }
        String filePathForWrite = rootDir + File.separator + fileName;
        return filePathForWrite;

    }

}

And here is the string array at the input of AsyncTask:
["http://***.ru/todbook/book_1/pages/page1.jpg","http://***.ru/todbook/book_2/pages/page1.jpg","http://***.ru/todbook/book_3/pages/page1.jpg","http://***.ru/todbook/book_4/pages/page1.jpg","http://***.ru/todbook/book_5/pages/page1.jpg","http://***.ru/todbook/book_6/pages/page1.jpg","http://***.ru/todbook/book_7/pages/page1.jpg","http://***.ru/todbook/book_8/pages/page1.jpg","http://***.ru/todbook/book_9/pages/page1.jpg"]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Comatu, 2018-02-18
@Comatu

It's most likely an emulator glitch. After restarting it everything works as it should.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question