Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copyFileTo creates subdirectory in target directory with target's name #105

Closed
Toprakete opened this issue Nov 28, 2022 · 7 comments
Closed
Labels
bug Something isn't working

Comments

@Toprakete
Copy link

Hi,
first let me thank you for this great library.

Library version: 1.5.1
OS version: [Android 12]
Device model: Realme 7

In my app the user can choose a file (mStorageHelper.openFilePicker()), after this the selected file is copied to the apps internal storage and user can edit it in the app. After editing the user can finish editing and copy back the file to the original place.
When I copy the file back from the apps storage to the original folder, the copyFile methode creates a subfolder in the target

File sourceDBFile = new File(data.m_filename);
// app internal filename data.m_filename = "/data/user/0/xxx.xxx.xxx/files/projects/yyy.db"
DocumentFile source = DocumentFile.fromFile(sourceDBFile);

DocumentFileUtils.copyFileTo(source, ctx, data.m_localImportSourcePath, null, new FileCallback() ...
// with data.m_localImportSourcePath = "/storage/emulated/0/test", this is the original folder

Result is the copied file yyy.db is in the folder /storage/emulated/0/test/test with absolute path
/storage/emulated/0/test/test/yyy.db

If I try to copy to the targets parent I get the error "CANNOT_CREATE_FILE_IN_TARGET", of course I only have the permission for the "test" folder
How can I prevent this? Is this the intented behaviour?
Thx and best reagrds
Klaus

@anggrayudi
Copy link
Owner

You can override onParentConflict() or onContentConflict() to replace the existing folder. Read the example here:

@Toprakete
Copy link
Author

Hi,
thx for your fast answer.
I use the copyFileTo function which does only provide a FileCallback with "onConflict", which I use to always replace the existing file
@OverRide
public void onConflict(@nonnull DocumentFile destinationFile, @nonnull FileConflictAction action) {
//super.onConflict(destinationFile, action);
// replace
action.confirmResolution(ConflictResolution.REPLACE);
}
But this i not called, because the originla file is on level up an so there's no conflict
Do you have another idea?
thx and br
Klaus

@Toprakete
Copy link
Author

Ok, I tried to write directly in the target directory and got a permission denied, even this folder has storage access (SimpleStorage.hasStorageAccess retruns true)
The problem is that the permission is on the tree-url and not the single url, when creating DocumentFile fromFile.

It all work when I use instead of "data.m_localImportSourcePath" ("/storage/emulated/0/test") the corresponding Tree-Uri from the getPersistedUriPermissions result List
Using this, it works like exspected.

The problem is to make the Tree-Uri from the getPersistedUriPermissions list comparable with the single tree uri from the picker result. I didn't find a nice way, so I compare both last segments of the uris. no idea wether there's another better solution.

Sorry for wasting your time. Please close, if this is the wanted way to go.

br
Klaus

@anggrayudi anggrayudi added the bug Something isn't working label Nov 28, 2022
@anggrayudi
Copy link
Owner

Hi, this is a bug. I'll fix it in v1.5.2

@anggrayudi
Copy link
Owner

anggrayudi commented Nov 28, 2022

As a temporary solution, please go ahead with copyFileTo(Context, DocumentFile, FileDescription, FileCallback):

DocumentFile source = DocumentFile.fromFile(sourceDBFile);
// Parameter requiresWriteAccess must be "true", because you're going to write files into it
DocumentFile targetFolder = DocumentFileCompat.fromFullPath(this, data.m_localImportSourcePath, DocumentFileType.ANY, true);
DocumentFileUtils.copyFileTo(source, ctx, targetFolder, null, new FileCallback());

@Toprakete
Copy link
Author

Hi,
thx so much.
br
Klaus

anggrayudi added a commit that referenced this issue Nov 28, 2022
anggrayudi added a commit that referenced this issue Dec 7, 2022
@anggrayudi
Copy link
Owner

Fixed in v1.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants