Android: Open file in local network - android

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 :)

Related

Reading and existing file in Xamarin withC#

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();
}

Reading file logged via slf4android?

I'm writing logs using slf4android (https://github.com/bright/slf4android) but it is not obvious how to read them (ideally I would just like to download them to my computer). The internal storage of the app is not accessible to other apps. Can I configure the slf4android to log to a shared directory? I've tried this but I get NOENT:
FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
File lol = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
fileHandler.setFullFilePathPattern(fileHandler.toString() + "/my_log.%g.%u.log");
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
Once you configured logging to a file by:
FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
fileHandler.setFullFilePathPattern("/sdcard/your.package/my_log.log");
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
There are two (both simple) ways to get a log file:
Using NotifyDeveloperHandler (my favourite)
slf4android has a very nice feature (for some reason undocumented) which allows sending an email to a given address with logs and screenshot included in an attachment.
NotifyDeveloperHandler handler = LoggerConfiguration.configuration().notifyDeveloperHandler(this, "example#gmail.com");
handler.notifyWhenDeviceIsShaken();
LoggerConfiguration.configuration().addHandlerToRootLogger(handler);
it's really handy to use (literally) because you can trigger a send action by shaking your device.
Using adb
Run adb pull /sdcard/your.package/my_log.log ~/my_log.log in terminal to copy log file from the device to home directory.
The official docs says you can do this:
FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
fileHandler.setFullFilePathPattern(SOMEPATH);
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
and the log file would be located into SOMEPATH. I would recommend you use a regular environment directory instead of an arbitrary string, like
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath()+File.pathSeparator+"appLogs"
Now, if you want to copy some existing logs to an outher destination, you can just copy the files.
if(BuildConfig.DEBUG) {
File logs = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath(), "logs");
FileLogHandlerConfiguration fileHandler = LoggerConfiguration.fileLogHandler(this);
LoggerConfiguration.configuration().addHandlerToRootLogger(fileHandler);
File currentLogs = fileHandler.getCurrentFileName();
if (currentLogs.exists()) {
FileChannel src = new FileInputStream(currentLogs).getChannel();
FileChannel dst = new FileOutputStream(logs).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
Finally, keep in mind nothing will work if you don't get the proper storage permissions!
Hope it helps. Happy coding!
In code example your provided you don't actually use "File lol" you've defined.
So it probably fails because you try to create another log on top of the first one (e.g. in "/sdcard/your.package/my_log.%g.%u.log/my_log.%g.%u.log");
Try:
File lol = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
fileHandler.setFullFilePathPattern(lol.getAbsolutePath() + "/my_log.%g.%u.log");
But you can also just add a menu option clicking which would find the logs, may be zip them (together with db or whatever) and send by email, upload to server or just copy to another folder.

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

How can I write a public file for my service to pick up on Android?

I have two parts to this question: 1) what is the best solution to my need, and 2) how do I do this?
1) I have a client app which sends bundles to a service app. the bundles can break the limit on bundle size, so I need to write the actual request out and read it in on the service side. Because of this, I can't write to my private internal storage. I've used these pages heavily, and haven't had luck: http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/training/basics/data-storage/files.html
My current understanding is that my best path is to use this to get a public dir:
File innerDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
I then add in my filename:
String fileName = String.valueOf(request.timestamp + "_avoidRoute"+count+++".ggr");
Combing these two results in the full file path:
/storage/emulated/0/Download/GroundGuidance/Route/1425579692169_avoidRoute1.ggr
Which I write to disk like this:
fos = context.openFileOutput(fullPath, Context.MODE_WORLD_WRITEABLE);
fos.write(routeString.getBytes());
fos.close();
When I try to write this to disk I get the error
java.lang.IllegalArgumentException: File /storage/emulated/0/Download/GroundGuidance/Route/1425579692169_avoidRoute1.ggr contains a path separator
Of course it does - I need it to have a path. I've searched online for solutions to this error which tell me to us FileOutputStream to write a full path. I did, but while my app doesn't error and appears to create the file, I'm also not able to view it on my phone in Windows Explorer, leading me to believe that it is creating a file with private permissions. So this brings me to my post and two questions:
1) Is there a different approach I should be trying to take to share large amounts of data between my client and service apps?
2) If not, what am I missing?
Thanks all for reading and trying to help!
Combing these two results in the full file path:
/storage/emulated/0/Download/GroundGuidance/Route/1425579692169_avoidRoute1.ggr
Which I write to disk like this:
fos = context.openFileOutput(fullPath, Context.MODE_WORLD_WRITEABLE);
This is not an appropriate use of Context's openFileOutput() method as that does not take a full path, but rather a filename within an app's private storage area.
If you are going to develop a full path yourself, as you have, then use
fos = new FileOutputStream(fullPath)
The Sharing permission setting is not applicable to the External Storage, though you will need a manifest permission to write (and implicitly read) on your creator, and the one for reading on your consumer.
Or, instead of constructing a full path, you could use your private storage with a filename and Context.MODE_WORLD_WRITEABLE (despite the being deprecated as an advisory) and pass the absolute path of the result to the other app to use with new FileInputStream(path).
Or you could use any of the other data interchange methods - content providers, local sockets, etc.

Check if a file has been written to in Android

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(...).

Categories

Resources