Hi When I am trying to download a file from FTP Server in android ,The file is getting download but it is corrupted.
I have attached my code for download.
{FileOutputStream br = new FileOutputStream(DestPath);
mFTPClient
.setFileTransferMode(FTP.BINARY_FILE_TYPE);
status = mFTPClient.retrieveFile(srcFilePath,br);
}
Can anyone tell me what is the issue.
You're not using the write method. setFileTransferMode is for transfer modes. setFileType is for setting file types.
.setFileType(FTP.BINARY_FILE_TYPE);
See the docs for setFileType and setFileTransferMode.
Related
This code works:
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "File.text");
File.ReadAllText(file);
Pretty straight-forward, but when I display the variable "file" using a Pizel 5 simulator I get a weird path: /data/user/0/com.companyname.aikidohours/files/.local/share/File.text. I can write new data to the file and read from it. but I now want to read from an existing file and I can't figure out where to put the file. Can someone tell where to put a text file full of basic information that I need to read from in Xamarin for adnroid?
Thanks
Todd
If you want to deploy that file with your application, you can add it to the 'Assets' folder inside your_project_name.Android project.
WARNING!!! File put in this folder are READONLY!
In case this is ok for you,this is the code to access the file:
using Xamarin.Essentials;
using (var reader = new StreamReader(await FileSystem.OpenAppPackageFileAsync("file_path")))
{
string content = reader.ReadToEnd();
}
I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).
To edit a Google Drive native doc on my Android app, I'm downloading the file to a .docx format (converting), editing the file in an editor capable of editing MS Word docx files, then I'm uploading the file back to its native Google doc format.
I'm using the following code:
File liveFileInfo = sService.files().get(mItem.getFileId()).execute();
mInputStream = new FileInputStream(pFile); //pFile is the java.io.File of the source XXX.docx file
InputStreamContent mediaContent = new InputStreamContent("application/vnd.openxmlformats-officedocument.wordprocessingml.document",
new BufferedInputStream(mInputStream));
mediaContent.setLength(pFile.length());
Drive.Files.Update update = sService.files().update(mItem.getFileId(), liveFileInfo, mediaContent);
update.setConvert(true);
MediaHttpUploader uploader = update.getMediaHttpUploader();
int chunkSize = getChunkSize(mItem);
uploader.setChunkSize(chunkSize);
uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
#Override
public void progressChanged(MediaHttpUploader uploader)
throws IOException {
// update UI
}
});
File uploadedFile = update.execute();
The code runs without errors, but the contents of the file contains all the weird characters you normally see when the wrong format is uploaded or an incorrect file type.
So my question is whether it is possible to update an existing file using this process? Or is the conversion process only supported with INSERTed files? Is there anything wrong with the code above?
I figured out what I missed. Add the following line to the code above to fix the problem:
liveFileInfo.setMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
I'm trying to create a program that's send SMSs every x minutes, using a .txt file located in a shared folder on my LAN network, I can't find how to catch this file...
I want to read it remotely or at least download it to my SDcard to read it after.
I tryed to solve it with few ways, but no one worked
File f = new File("file://192.168.2.106/Users/Maxime.test.txt");
or:
String textSource = "\\\\192.168.2.106\\Users\\Maxime\\test.txt";
textUrl = new URL(textSource);
My sharing does not requires a password and it's visible with ES Explorator
Thank you for your help :)
I am building an android application and have the following problem. When I want to open a file from the sdcard using FileInputStream and if the file name contains a blank character, I get the FileNotFoundException. If there are no blank characters, everything works fine. So, if I am to open for example: "My file.ext" it raises an exception, and if the file name is "Myfile.ext" everything is fine.
Any clues on how to resolve that will be much appreciated.
Regards
You will need to use an escape character to specific that there is a blank in the file name. I believe it is a '\'...
So it would be like this "my\ textfile.txt"
You could try to let android parse a certain file and open it
File file = new File(Uri.parse(path+"file name.txt"));
FileInputStream inputStream = new FileInputStream(file);
Uri should parse your filenames correctly.