I'm building an android app which finds the nearby wifi hotspots. I have an xml file which includes GeoLocations of the hotspots, the xml file is located in the res/xml folder of the application. I would like to download an updated xml file when application starts. I have two questions:
Shoud I download the xml file into Internal Storage or External Storage and why? If I should download the xml file into Internal Storage, how can I do that?
I would like the user to be able to use the initially embedded xml file when the application first starts and switch to the newly download xml file when the download finishes. How can I achieve that seamlessly?
Thanks
Take a look at Google's page regarding data storage on Android.
http://developer.android.com/guide/topics/data/data-storage.html
You can download the XML file in a pretty straightforward matter. Another question like yours was asked here:
Download a file with Android, and showing the progress in a ProgressDialog
In that example, the file gets saved to the SD card. Be sure to change the OutputStream pointing to the SD card, so that it points to the private storage, as the reference page describes:
FileOutputStream fos = openFileOutput(your_xml_filename, Context.MODE_PRIVATE);
To make the switch to the new XML as simple as possible, I'd encapsulate all XML-related functionality in one class and provide a simple method for switching to the new XML. For example,
class XMLUser {
private Location someLocation;
public XMLUser(String pathToXml) {
readxmlAndSetLocations(pathToXml);
}
}
Then you'd initialize a new XMLUser object whenever you'd need to change the XML file. Like so:
XMLUser xmlStuff = new XMLUser(PATH_TO_RES_FILE);
//... do something with it
//... update found
XMLUser xmlStuff = new XMLUser(PATH_TO_PRIVATEDATA_FILE);
Another way would be to use the same object and just update the locations from the XML file:
class XMLUser {
private Location someLocation;
public void updateLocations(String pathToXml) {
//Read your xml here and update the locations accordingly.
}
}
e.g.
XMLUser xmlStuff = new XMLUser(PATH_TO_RES_FILE);
//... do something with it
//... update found
xmlStuff.updateLocations(PATH_TO_UPDATED_XML);
Your questions allow for a wide variety of answers so I can't give a quick and exact one.
And yes, I do recommend saving the XML data to the internal storage. The XML file should be small enough in size to not pose problems, and logically, it's related to the internal functionality of the app. With which the user should not tamper. Plus, the internal storage is not removable, so in case the user changes the SD card, the updated data will still be there.
Related
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.
I wanna include a text file to my project in smartface appstudio. I put it where? In resources folder or assets? FileStream could not read it in resources (as a drawable item). Any idea?
var txtFile = new SMF.IO.FileStream(SMF.IO.applicationResources, "words.txt", SMF.IO.StreamType.read);
txtFile.readToEnd();
It is not implemented in current release of Smartface App Studio. Also I have tried it before. It is Phase 2 for File Operations.
You can download your static text file from the internet and can save it to the local storage. And read it from there. I can just suggest you this idea. Check some helpful links below.
http://developer.smartface.io/hc/en-us/articles/203177557-File-Operations
http://developer.smartface.io/hc/en-us/articles/202153536-File-Download-Upload
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(...).
I've researched this problem for a while now and I've only found really complicated answers so I'm very confused. Keep in mind that I'm not an expert programmer so don't expect me to know a ton about this!
All I want to do is print a new line of characters to a text file located in the downloads folder of an SD card in Android. I set up my emulator to have an SD card and placed the text file in the downloads folder. This piece of code is for a database class that will access a text file in an SD card to read the data. I know that the class works outside of Android so assume that all of the methods are working as they should to read the data!
I get an IOException when I run this method in another class:
public void addRecordToDataBase(ChildRecord c) throws IOException
{
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
PrintWriter out = new PrintWriter(outFile);
out.println(c.printToDataBase());
out.close();
}
The weird thing is I can read from the database just fine in other methods using that same path; no problems there. I just can't write to it. I've read somewhere that you can use "regular Java methods" to write to an SD card in Android without those crazy "OutputStream" things all over the place. Is this true? I debugged this thing and found out that the line of code that is throwing the exception is right here:
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
If anyone has any idea why I'm geting this IOException, I would be really grateful! I did try all the crazy methods that Android wants to use but I think I got lost in it so I just reverted back to what I knew how to do.
Thank you so much!
My guess is you can't access such directory with writing permissions, at least in that manner.
Did you take a look at http://developer.android.com/guide/topics/data/data-storage.html#filesExternal?
That reference and this one (http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)), explaining how getExternalFilesDir works, may be of help to you!
Have you declared the WRITE_EXTERNAL_PERMISSION permission in your apps manifest file?
I would like to make a file browser that will do two things:
1) Allow the user to browse and select a directory
2) Allow the user to browse all files on their sdcard
I've looked for tutorials but can't seem to find any?
Can someone please help me by either explaining how what my code would need to do in order to have a simple file browser or providing me with a link to a tutorial/source code?
Please and thanks!
If you're actually more interested in learning to write your own, I'd suggest taking a good long read through the File class documentation. That's where you're going to be doing most of the work.
In the case of SD cards/other external storage for Android, you'll want to first check to ensure that the external storage is mounted and available before trying to read it, using the Environment class:
String extState = Environment.getExternalStorageState();
//you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY)
//if you are only interested in reading the filesystem
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
//do your file work here
}
Once you've determined the proper state of the external storage, a simple way to start is to use File's listFiles() method, like so:
//there is also getRootDirectory(), getDataDirectory(), etc. in the docs
File sd = Environment.getExternalStorageDirectory();
//This will return an array with all the Files (directories and files)
//in the external storage folder
File[] sdDirList = sd.listFiles();
You can then start using FileFilters to narrow down your results:
FileFilter filterDirectoriesOnly = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] sdDirectories = sd.listFiles(filterDirectoriesOnly);
From there on, just read through the docs to find the type of thing you're looking to do with it, and then you can work on tying these into list adapters, etc.
Hope this helps!
This is a late answer but I worked on creating an android file explorer recently. https://github.com/mburman/Android-File-Explore
Its really straightforward. Essentially its just 1 file that you would need to integrate into your application.
Take a look at OI File Manager, which is an open-source Android file manager. You can get the source code here.