diff --git a/README.md b/README.md index ca8b22cedc83da0a892a2929ab2ffca1d7498359..1d1ae66a210f7eabc43ce2cfffe34c728c3532a1 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ dialog.setDialogSelectionListener((files) -> { File file = new File(path); ListItem item = new ListItem(); item.setName(file.getName()); - item.setPath(file.getAbsolutePath()); + item.setPath(file.getCanonicalPath()); listItem.add(item); } mFileListAdapter.notifyDataSetItemRangeInserted(0, listItem.size()); diff --git a/entry/src/main/java/com/developer/filepicker/file/slice/MainAbilitySlice.java b/entry/src/main/java/com/developer/filepicker/file/slice/MainAbilitySlice.java index bdbfca79c3e993fa279a0563679205ec05e8ec35..be93a8ab91e0b61ec0dc8463aa9be72eab435fef 100644 --- a/entry/src/main/java/com/developer/filepicker/file/slice/MainAbilitySlice.java +++ b/entry/src/main/java/com/developer/filepicker/file/slice/MainAbilitySlice.java @@ -19,6 +19,7 @@ import ohos.agp.window.dialog.ToastDialog; import ohos.agp.window.service.WindowManager; import ohos.app.Context; import ohos.app.Environment; +import ohos.data.resultset.ResultSet; import ohos.global.resource.RawFileEntry; import ohos.global.resource.Resource; import ohos.global.resource.ResourceManager; @@ -196,7 +197,11 @@ public class MainAbilitySlice extends AbilitySlice { File file = new File(path); ListItem item = new ListItem(); item.setName(file.getName()); - item.setPath(file.getAbsolutePath()); + try { + item.setPath(file.getCanonicalPath()); + } catch (IOException e) { + e.printStackTrace(); + } listItem.add(item); } mFileListAdapter.notifyDataSetItemRangeInserted(0, listItem.size()); diff --git a/library/src/main/java/com/developer/filepicker/utils/Utility.java b/library/src/main/java/com/developer/filepicker/utils/Utility.java index 607445c86e2e576fbb0b21958ec7cc94eb688ea4..4ebea679f09bf552f4a0c674fc4f44ce98b3a731 100644 --- a/library/src/main/java/com/developer/filepicker/utils/Utility.java +++ b/library/src/main/java/com/developer/filepicker/utils/Utility.java @@ -5,6 +5,7 @@ import ohos.app.Context; import ohos.bundle.IBundleManager; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Objects; @@ -36,7 +37,11 @@ public class Utility { FileListItem item = new FileListItem(); item.setFilename(name.getName()); item.setDirectory(name.isDirectory()); - item.setLocation(name.getAbsolutePath()); + try { + item.setLocation(name.getCanonicalPath()); + } catch (IOException e) { + e.printStackTrace(); + } item.setTime(name.lastModified()); internalList.add(item); } diff --git a/library/src/main/java/com/developer/filepicker/view/FilePickerDialog.java b/library/src/main/java/com/developer/filepicker/view/FilePickerDialog.java index 19e05e329cc994f9cb8b57f61238baea0ee7d1f4..ebdde0fa6631de9607c3cecfa2117e2536d822a2 100644 --- a/library/src/main/java/com/developer/filepicker/view/FilePickerDialog.java +++ b/library/src/main/java/com/developer/filepicker/view/FilePickerDialog.java @@ -182,7 +182,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item } } - public void onStart() { + public void onStart() throws IOException { positiveBtnNameStr = ( positiveBtnNameStr == null ? "Select" : @@ -193,21 +193,21 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item File currLoc; internalList.clear(); if (properties.offset.isDirectory() && validateOffsetPath()) { - currLoc = new File(properties.offset.getAbsolutePath()); + currLoc = new File(properties.offset.getCanonicalPath()); FileListItem parent = new FileListItem(); parent.setFilename(".."); parent.setDirectory(true); parent.setLocation(Objects.requireNonNull(currLoc.getParentFile()) - .getAbsolutePath()); + .getCanonicalPath()); parent.setTime(currLoc.lastModified()); internalList.add(parent); } else if (properties.root.exists() && properties.root.isDirectory()) { - currLoc = new File(properties.root.getAbsolutePath()); + currLoc = new File(properties.root.getCanonicalPath()); } else { - currLoc = new File(properties.error_dir.getAbsolutePath()); + currLoc = new File(properties.error_dir.getCanonicalPath()); } dname.setText(currLoc.getName()); - dir_path.setText(currLoc.getAbsolutePath()); + dir_path.setText(currLoc.getCanonicalPath()); setTitle(); internalList = Utility.prepareFileListEntries(internalList, currLoc, filter, @@ -218,9 +218,9 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item } } - private boolean validateOffsetPath() { - String offset_path = properties.offset.getAbsolutePath(); - String root_path = properties.root.getAbsolutePath(); + private boolean validateOffsetPath() throws IOException { + String offset_path = properties.offset.getCanonicalPath(); + String root_path = properties.root.getCanonicalPath(); return !offset_path.equals(root_path) && offset_path.contains(root_path); } @@ -233,14 +233,22 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item File currLoc = new File(fitem.getLocation()); dname.setText(currLoc.getName()); setTitle(); - dir_path.setText(currLoc.getAbsolutePath()); + try { + dir_path.setText(currLoc.getCanonicalPath()); + } catch (IOException e) { + e.printStackTrace(); + } internalList.clear(); if (!currLoc.getName().equals(properties.root.getName())) { FileListItem parent = new FileListItem(); parent.setFilename(".."); parent.setDirectory(true); - parent.setLocation(Objects.requireNonNull(currLoc - .getParentFile()).getAbsolutePath()); + try { + parent.setLocation(Objects.requireNonNull(currLoc + .getParentFile()).getCanonicalPath()); + } catch (IOException e) { + e.printStackTrace(); + } parent.setTime(currLoc.lastModified()); internalList.add(parent); } @@ -286,7 +294,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item } } - public void markFiles(List paths) { + public void markFiles(List paths) throws IOException { if (paths != null && paths.size() > 0) { if (properties.selection_mode == DialogConfigs.SINGLE_MODE) { File temp = new File(paths.get(0)); @@ -298,7 +306,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item item.setDirectory(temp.isDirectory()); item.setMarked(true); item.setTime(temp.lastModified()); - item.setLocation(temp.getAbsolutePath()); + item.setLocation(temp.getCanonicalPath()); MarkedItemList.addSelectedItem(item); } break; @@ -310,7 +318,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item item.setDirectory(temp.isDirectory()); item.setMarked(true); item.setTime(temp.lastModified()); - item.setLocation(temp.getAbsolutePath()); + item.setLocation(temp.getCanonicalPath()); MarkedItemList.addSelectedItem(item); } break; @@ -322,7 +330,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item item.setDirectory(temp.isDirectory()); item.setMarked(true); item.setTime(temp.lastModified()); - item.setLocation(temp.getAbsolutePath()); + item.setLocation(temp.getCanonicalPath()); MarkedItemList.addSelectedItem(item); } break; @@ -338,7 +346,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item item.setDirectory(temp.isDirectory()); item.setMarked(true); item.setTime(temp.lastModified()); - item.setLocation(temp.getAbsolutePath()); + item.setLocation(temp.getCanonicalPath()); MarkedItemList.addSelectedItem(item); } break; @@ -350,7 +358,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item item.setDirectory(temp.isDirectory()); item.setMarked(true); item.setTime(temp.lastModified()); - item.setLocation(temp.getAbsolutePath()); + item.setLocation(temp.getCanonicalPath()); MarkedItemList.addSelectedItem(item); } break; @@ -363,7 +371,7 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item item.setDirectory(temp.isDirectory()); item.setMarked(true); item.setTime(temp.lastModified()); - item.setLocation(temp.getAbsolutePath()); + item.setLocation(temp.getCanonicalPath()); MarkedItemList.addSelectedItem(item); } break; @@ -420,14 +428,22 @@ public class FilePickerDialog extends CommonDialog implements ListContainer.Item } else { dname.setText(currLoc.getName()); - dir_path.setText(currLoc.getAbsolutePath()); + try { + dir_path.setText(currLoc.getCanonicalPath()); + } catch (IOException e) { + e.printStackTrace(); + } internalList.clear(); if (!currLoc.getName().equals(properties.root.getName())) { FileListItem parent = new FileListItem(); parent.setFilename(".."); parent.setDirectory(true); - parent.setLocation(Objects.requireNonNull(currLoc.getParentFile()) - .getAbsolutePath()); + try { + parent.setLocation(Objects.requireNonNull(currLoc.getParentFile()) + .getCanonicalPath()); + } catch (IOException e) { + e.printStackTrace(); + } parent.setTime(currLoc.lastModified()); internalList.add(parent); }