There is:
List<File> mImg = new ArrayList<>(); File dir = new File(Environment.getExternalStorageDirectory(),"DIR/"); File[] fileArray = dir.listFiles(); mImg.addAll(Arrays.asList(fileArray)); The application implemented Drag & Drop, dragging and swapping pictures. After exiting the application, the pictures are returned to their original position. As I understand it, it is necessary to save the ArrayList lines to a file (for example, txt) before exiting, and to get it back on startup. There are no problems with saving, but I can’t fill the List back.
Save:
void writeFileSD() { if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Log.i(LOG_TAG, "SD-карта не доступна: " + Environment.getExternalStorageState()); return; } File sdPath = Environment.getExternalStorageDirectory(); sdPath = new File(sdPath.getAbsolutePath() + "/" + DIR_SD); sdPath.mkdirs(); File sdFile = new File(sdPath, FILENAME_SD); try { BufferedWriter bw = new BufferedWriter(new FileWriter(sdFile)); bw.write(mImg.toString()); bw.close(); Log.i(LOG_TAG, "Файл записан на SD: " + sdFile.getAbsolutePath()+"text "+mImg); } catch (IOException e) { e.printStackTrace(); } } We read:
void readFileSD() { if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Log.i(LOG_TAG, "SD-карта не доступна: " + Environment.getExternalStorageState()); return; } File sdPath = Environment.getExternalStorageDirectory(); sdPath = new File(sdPath.getAbsolutePath() + "/" + DIR_SD); File sdFile = new File(sdPath, FILENAME_SD); try { BufferedReader br = new BufferedReader(new FileReader(sdFile)); String data = ""; while ((data = br.readLine()) != null) { Data.add(data); Log.i(LOG_TAG,"Читаем " + data+"Список "+Data); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } Read the file and fill in the List<String> Data = new ArrayList<String>(); lines (file paths). You must transfer the List<File>. Как преобразовать? files to the adapter List<File>. Как преобразовать? List<File>. Как преобразовать?
List<File> mImg, and then simply load these paths, createFileobjects using them and add them to theArrayList. - post_zeew