I am making a file backup program, using ACTION_OPEN_DOCUMENT_TREE my user picks a folder, then my intent is to copy all items in that folder to the tablet, I do this by saving the URI of the folder the user selects in variable TheURI, I then iterate between all files in the folder by the following
DocumentFile documentFile = DocumentFile.fromTreeUri(this, TheURI);
for (DocumentFile file : documentFile.listFiles()) {
Log.i("",file.getName());
if(file.isDirectory()){
Log.i("","is a Directory");
}else{
if(file.canRead()||file.canWrite()){
savefile(file.getUri());
}
Log.i("","is not a Directory");
}
Log.i("file.canRead(): " , String.valueOf(file.canRead()));
Log.i("file.canWrite(): " , String.valueOf(file.canWrite()));
Log.i("", String.valueOf(file.getUri()));
}
My savefile function uses SimpleStorage api to grab the absolute file path as follows:
DocumentFile Filename = DocumentFileCompat.fromUri(this, sourceuri);
DocumentFileUtils.copyFileTo(Filename, this, "/sdcard/Left", null, new FileCallback() {
@Override
public void onConflict(@NotNull DocumentFile destinationFile, @NotNull FileCallback.FileConflictAction action) {
// do stuff
}
@Override
public void onCompleted(@NotNull Object result) {
if (result instanceof DocumentFile) {
// do stuff
} else if (result instanceof MediaFile) {
// do stuff
}
}
@Override
public void onReport(Report report) {
Log.d("%s", String.valueOf(report.getProgress()));
}
@Override
public void onFailed(ErrorCode errorCode) {
Log.d("Error: %s", errorCode.toString());
}
});
I am copying a 500mb file to a tablet that has 20gb of free storage however I get the error
D/Error: %s: NO_SPACE_LEFT_ON_TARGET_PATH
solution found!
When writing to a directory, "/sdcard/" is not a valid path (Most absolute paths aren't) however, if you point directly to the primary storage, which should always be in 'storage/emulated/0' then there will be space. This is pointed out in the SimpleStorage readme, I just missed it!
Turns out grabbing the absolute path still requires you to specify that you're putting it in emulated, because self is always treat as full