before android 10 i can restore my database from any folder now when i create my fileinputstream and error becomes: /storage/emulated/0/Download/bd/bd.sqlite: open failed: EACCES (Permission denied), I dont understan how give permision with MediaStore
FileInputStream fis = new FileInputStream(bdOrigen);
OutputStream output = new FileOutputStream(PathDestino);
// transferir bytes al backup
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
No simple answer here, you will have to start reading up on Storage Access Framework as i think Scoped Storage will not meet your requirements
Related
I am trying to upload a file in android to an FTP server using the apache.commons.ftp library.
Note: I am using storeFileStream so that I can track the progress of the upload.
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = new FileInputStream(localFile);
OutputStream outputStream = ftpClient.storeFileStream(remoteFile);
byte[] buffer = new byte[8192];
int bytesRead;
logMessage("Starting to upload file");
while ((bytesRead = inputStream.read(buffer)) != -1) {
total += bytesRead;
outputStream.write(buffer, 0, bytesRead); //output stream is null here
latestPercentDone = (int) ((total / (float) totalMegaBytes) * 100);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
publishProgress(""+percentDone);
}
}
inputStream.close();
outputStream.close();
ftpClient.completePendingCommand();
I am using the speedtest.tele2.net server for testing. Using an anonymous login.
The log keeps saying the outputstream is null.
EDIT: Using what Martin Prikryl and getting the error code, i found the problem to be the location of the remote file.
Upload to a location rather than a directory.
For example if the file is caled item1.txt the remote file should be /item1.txt or /someFolder/AnotherFolder/item1.txt rather than /someFolder/AnotherFolder/
The FTPClient.storeFileStream method can return null:
An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
Though the "file does not exist" note is nonsense for upload, it's obviously a copy-and-paste error from the .retrieveFileStream (download).
Use the .getReplyCode and .getReplyString, to see what went wrong.
This was originally a part 2 of a different thread, but another use suggested that I separate the part 2 into it's own topic, so here we go. Original thread is here (Original Thread)
I am using Jackcess to create a V2010 mdb file that I need to transfer to a client that will use Access 2013 to open it. Jackcess itself works - V2010 creates a file that Access 2013 can open when the file is FTP'd to the client by a third party software, such as FAR. However, when I try to upload this file to the client through the servlet (as is the goal of this project), Access on the client says "Unrecognized database format "...file name...". This is the code used for upload. Code itself works, file is transferred, has a non-zero size if it's saved - but Access cannot open it.
Note, for content type I also tried vnd.msassess and octed-stream, with same unsuccessful results. Also, I tried closing the db and creating the FileInputStream from the file name, and, as in the example, tried to create FileInputStream by calling mydb.getFile(). No difference.
response.setContentType("application/vnd.ms-access");
String fileName = "SomeFileName.mdb";
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
Database mydb = generateMDBFile();
FileInputStream fis = new FileInputStream(mydb.getFile());
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
try {
int byteRead = 0;
while ((byteRead = fis.read()) != -1) {
os.write(buffer, 0, byteRead);
}
os.flush();
} catch (Exception excp) {
excp.printStackTrace();
} finally {
os.close();
fis.close();
}
Why does this code corrupt the mdb file? This happens every time, regardless of the size (I tried a tiny 2 column/1 row file, and a huge file with 40 columns and 80000 rows)
Thank you!
You forgot to fill the buffer.
Use
// ...
while ((byteRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, byteRead);
}
// ...
I'm trying to write inputstream to a file, but it never gets written on disk, I just get the error file doesnt exist. The file I open is a drawable icluded in the project, I would like to save it to sd card. This is what I have so far:
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tester");
InputStream inputStream = getResources().openRawResource(R.drawable.test);
OutputStream out = new FileOutputStream(new File(storagePath, "test.png"));
byte buffer[] = new byte[900];
int len;
while ((len = inputStream.read(buf)) > 0)
out.write(buffer, 0, len);
out.close();
inputStream.close();
Your tester directory doesn't exist. Check for it and create it if necessary before opening your FileOutputStream.
I managed to create zip file in java using this simple piece of code:
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
out.setMethod(ZipOutputStream.DEFLATED);
out.setLevel(5);
byte data[] = new byte[BUFFER];
FileInputStream fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(file.getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
out.closeEntry();
origin.close();
out.close();
zip file created successfully. However when I try to unzip it using WinZip or some other tools I get an error:
Central and local directory mismatch for file "my_file" (general
purpose flags - local:808 hex central: 8 hex).
Severe Error: Local and central GPFlags values don't match.
What's really weird, WinRAR and internal Win7 zip do not show any errors when I decompress the file.
What am I doing wrong? Anybody had this issue?
Thanks a lot!
Must be that out.close is missing.
I think you forgot to close zipentry.
out.closeEntry();
origin.close();
out.close();
I need a sample code that copies files over Network to Local File System in Java.
How this is done in Java?
here is code that copies files in the local file system
File fromfile = new File("file");
File tofile = new File("../copiedfile");
tofile.createNewFile();
FileInputStream from = new FileInputStream(fromfile);
FileOutputStream to = new FileOutputStream(tofile);
byte [] buffer = new byte[4096];
int bytesread;
while ((bytesread = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesread);
}
I think, if you want to copy files over network you should send buffer using ObjectOutput and sockets