Application files path in android - java

I'm working on an android application, I need to get the path where the files generated by the app are stored, conosco that using this:
this.getFilesDir();
or I can get that folder through:
OutputStreamWriter fout=
new OutputStreamWriter(
openFileOutput("prueba_int.txt", Context.MODE_PRIVATE));
fout.write("Texto de prueba.");
fout.close();
and then:
getFileStreamPath("samplefile.txt");
I can get that route, but I am making a library and I need to place a series of files in the specified path without using parameters or the context of the activity.
there is way to get it?

there is way to get it?
No.
You will need to modify your library to accept something as input that will let you read and write your data where and how the app wants. Ideally, you use some sort of Java interface that allows the app some flexibility (e.g., stores your data in its own encrypted container), with you supplying a stock implementation that perhaps uses File from the app's own Context.

Related

create app with synchronization of remote files in Android studio

I am developing my app which contains some xml files used by some internal routines.
I would like to have the original xml files stored in a remote server, so that when a synchronization with such server is required by the user, the app updates the xml files, i.e. the version of the files present in remote is downloaded in local. Is it possible to realize such a setup? Is it possible to add a synchronize button inside the app? I develop in Android studio. For the moment, my app contains the local version of the xml files,
and the synchronization is missing.
Since these are not layout XML files,
Use something like this to download the file you need:
URL website = new URL("http://www.website.com/some_file.xml");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("some_file.xml");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
Put such block of code in the onClick method of the sync button
Modify the argument of the constructor of the FileOutputStream to save the file elsewhere
Save them somewhere like 'Environment.getExternalStorageDirectory() + "Android/package.name/files/some_file.xml'
And use the same path to get to the file and use it
I hope this helps
EDIT: As per your last comment, you need to identify each device using a uniqur identifier, which is usually hard to find being available on all android devices,I think you'd have to use Sign in/up system and thus means using PHP
Maybe there's better solution though

External Storage (public across apps)

I need to make a file in the Android External Storage area that can be accessed (read/write) by other apps.
I looked here but that says to pass it a directory type such as DIRECTORY_MUSIC, DIRECTORY_PICTURES etc.
But none of those types is what i want. So what I'm thinking is this:
File F = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyAppName" + File.separator + "MyFile");
Will this work or how can i ensure that a file will truly have public read/write access for other apps?
Also, I read somewhere that OutputStreams are only supposed to take a name with no path, and are always private, so how would i read/write ints to F?
Well, it seems to me that the statement for creating a new File you have there, would work. Although, why not try it out? Also, it is absolutely possible to write to a file and have it be accessible publicly, you do not need to do anything for this to happen, it will just happen by default. Reading/writing ints from/to the File can be done as usual, I recommend converting it to a String before writing.
It will works. You can ensure that a file truly have public access if you'll find it with some file manager and try to open it. In your example path to your file will be "%HOME_DIR%\MyAppName\MyFile". Also for creating file you could use FileOutputStream (not OutputStream), it takes File as parameter in constructor.
Files created in external storage are by default can be accessed by any other app (since the file is in external storage). The only thing you can control is the lifetime of the created file. As in whether the file should exist with your app only and deleted once the app is uninstalled or the file exists even after your app is removed.
If you want to create a file on your own path (not in DIRECTORY_PICTURE etc) inside your application then use getExternalFileDir() and pass null to it. See the link below for more details.
Saving file in external storage

How to load and save files in Android?

I need to load "configuration" type files for my program in Android, they are both .bin files containing dictionary data for the NLP library. I'm a bit new to Android still, and I'm having trouble finding a folder to place the files in so I can access them when the activity starts.
I also need to create/save/load a filetype specific to my program, and I don't know where to put it either. All I've been able to find on here is people using the getAssetManager() function to fetch input streams, but I explicitly need File objects for me to be able to load them into my pre-existing desktop software code I'd like to reuse (plus the libraries require them anyway)
I've also seen people using a "res/raw" folder, however the ADT did not generate this "raw" file when I made the project - so I'm not sure what to do there either.
Here is how I usually start the software in the desktop version, but I need to fetch these files in an Android environment:
brain.start(new File("memboric.core"), new File("en_pos_maxent.bin"), new File("en_sent.bin"));
core = brain.getInterpreter().getCore();
The memboric.core file can be generated, but I need to know WHERE and HOW to do so.
Thank you very much for your time, feel free to direct me to other resources if you feel this question is inadequate.
TLDR; how do I load "static" files for the software to function (needs to be included with software), and how to create/load/save "personal" files into an appropriate area of the device?
Use Context.getFilesDir(). Your application can read and write files in that folder and they'll automatically get deleted if your application gets uninstalled.
From that point forward, you can create, delete and read from files like any other Java application.
the "raw"-folder you can create it on your own. So check this out, which shows how to handle files in Android: http://developer.android.com/training/basics/data-storage/files.html

Creating a file just to be uploaded into the online form

couldn't find relevant question on SO I'm asking a new one. I can create a file using FileWriter class, but it requires to specify the path for that file (physically creates the file). What I want to achieve is to create a file like in-memory, without specyfying the path or saving it on the disk and then upload it into the online form with selenium webdriver, is that somehow possible?
But there also is another problem, html fileUpload element will accept the path such as:
driver.findElement(By.id("Content_CV")).sendKeys("C:\\Users\\name\\Documents\\my_cv.pdf");
but will it accept the file itself? Probably not, so assuming that some of you knows how to create a file without saving it on the disk, would there be a way of providing path to that file anyway (given its virtual location)?
I'm trying to figure it out and I did some google research, yet here I am. Thanks for any attempt of help :)
If your question is just how to create a temporary file without caring about how to name it and where to create it without overwriting existing files, then you can simply use File.createTempFile. This will create an actual new temporary file on disk in the directory designated by the operating system for that purpose. You might also want to have a look at File.deleteOnExit.
If you are not looking for a java solution, you can simply create the file in a RAM drive
Software for this exist for all mayor operating systems.
Have a look at the wikipedia link above for more information.

getClassLoader().getResource(filepath) returns a null pointer

I'm using a method to generate XML files dynamically for a research project, they get put into a loader that reads from a file path, I don't have any control over how the loader handles things (otherwise I'd pass the internal XML representation instead of monkeying with temp files), I'm using this code to save the file:
File outputs = File.createTempFile("lvlFile", ".tmp.xml");
FileWriter fw = new FileWriter(outputs);
fw.write(el.asXML());
fw.close();
// filenames is my list of file paths which gets returned and passed around
filenames.add(outputs.getAbsolutePath());
Now, I'm sure that the file in question is written to directly. If I print outputs.getAbsolutePath() and navigate there via terminal to check the files, everything is generated and written properly, so everything is correct on the filesystem. However, this code:
URL url = this.getClass().getClassLoader().getResource(_levelFile);
Where _levelFile is one of my filenames generated above, causes url to be null. The path isn't getting corrupted or anything, printing verifies that _levelFile points to the correct path. The same code has succeeded for other files. Further, the bug doesn't seem related to whether or not I use getPath(), getCanonicalPath(), or getAbsolutePath(), further setting outputs.isReadable(true) doesn't do anything.
Any ideas? Please don't offer alternatives to the Url url = structure, I don't have any control over this code*, I'm obligated to change my code so that the url is set correctly.
(*) At least without SIGNIFICANT effort rewriting a large section of the framework I'm working with, even though the current code succeeds in all other cases.
Edit:
Again, I can't use an alternative to the URL code, it's part of a loader that I can't touch. Also, the loading fails even if I set the path of the temp file to the same directory that my successfully loaded files come from.
I assume that the ClassLoader will only look for resources within the class path - which probably doesn't include /tmp. I'm not sure if it actually supports absolute path names. It might just interpret them as relative to the root of the individual class path.
How about using _levelFile.toURI().toURL() instead?
Your are creating file in file system and then trying to read it as a resource. Resource is where JVM takes its classes, i.e. the classpath. So this operation will work only if your are writing file into your classpath.
And even if this is correct be careful: if for example you are running from eclipse your process will not probably "see" the new resource until you refresh your workspace.
Now my question is: Are your really sure that you want to read files as resources. It seems that your just should create new FileInputStream(_levelFile) and read from it.
Edit
#Anonymouse is right. You are creating temporary file using 2-arg version of createTempFile(), so your file is created in your temporary directory. The chance that it is into your classpath is very low... :)
So, if you want to read it then you have to get its path or just use it when creating your input stream:
File outputs = File.createTempFile("lvlFile", ".tmp.xml");
..........................
InputStream in = new FileInputStream(ouptuts);
// now read from this stream.

Categories

Resources