Android Storage Access Framework - ParcelFileDescriptor - creates 0B file when writing file
I am currently trying to fetch an certain entire directory from my Linux server to
My android phone via sftp.
So far, My code worked fine.
However as soon as I built my app as Signed APK and installed it, Code below does not work as expected.
It doesn't download file from my Linux server but create 0B size files.
private void recursiveFolderDownload(String src, DocumentFile pickedDir) throws SftpException, InterruptedException {
Vector<ChannelSftp.LsEntry> fileAndFolderList = channelSftp.ls(src);
for (ChannelSftp.LsEntry item : fileAndFolderList) {
if (!item.getAttrs().isDir()) {
DocumentFile newFile = pickedDir.findFile(item.getFilename());
if(newFile==null){
MainActivity.changeLoadingMessage("Fetching "+item.getFilename());
restoreStatus = "Fetching "+item.getFilename();
newFile = pickedDir.createFile("",item.getFilename());
restoringFiles.add(getPathFromUri(newFile.getUri()));
write(src + "/" + item.getFilename(),newFile.getUri());
}
else{
MainActivity.changeLoadingMessage("Skipping "+item.getFilename());
restoreStatus = "Skipping "+item.getFilename();
}
} else if (!(".".equals(item.getFilename()) || "..".equals(item.getFilename()))) {
DocumentFile newDir = pickedDir.findFile(item.getFilename());
if(newDir==null){
newDir = pickedDir.createDirectory(item.getFilename());
}
recursiveFolderDownload(src + "/" + item.getFilename(), newDir);
}
}
}
private void write(String src, Uri uri) throws InterruptedException {
try {
ParcelFileDescriptor descriptor = context.getContentResolver().openFileDescriptor(uri,"w");
if(descriptor!=null) {
FileOutputStream fos=new FileOutputStream(descriptor.getFileDescriptor());
channelSftp.get(src,fos);
fos.close();
}
} catch (IOException | SftpException e) {
e.printStackTrace();
}
}
Of course I implemented Storage Access Framework by using activity with Intent.ACTION_OPEN_DOCUMENT_TREE
It's strange that this code works as non-release APK but not as release APK.
Anyway. Thank you so much for reading this question.
Hope I could find answer.
Source: View source