Android getFileDir Location - android

In android, I am trying to save the XML file locally.
I am using the below syntax to do it
File myFile = new File(getFilesDir()+"/loginUser.xml");
myFile.createNewFile();
It is working fine in emulator as well as device.
Now my question is what is the path this getFilesDir() in System(in Emulator)

See http://developer.android.com/guide/topics/data/data-storage.html#filesInternal.
It can depend on the system but generally files are stored somewhere in /data/data.. in separate directories for each app.

Related

Getting absolute path on android

I've got c++ code in which I try to get a file from a directory on an android device. I've tried different ways to set the path which I pass to the fopen() function like:
/Android/data/com.myapp/files/Blip.wav
There actually is this file. But I guess that this is not a proper way to write a path. (The example was obtained by the java code )
getContext().getApplicationContext().getFilesDir().getPath() + "/Blip.wav"
There actually is this file
Since I have never seen an Android device with an /Android directory, that is unlikely.
What would fit is if you are looking at /Android/data/com.myapp/files/Blip.wav in a desktop file manager, using a USB or similar connection. In that case, Android/data/com.myapp/files/Blip.wav is a relative path in external storage. Specifically, it maps to:
new File(getContext().getExternalFilesDir(), "Blip.wav")
Try using this.
File root=Environment.getExternalStorageDirectory();
File file=new File(root,"/PersonData/Blip.wav");
Here personData is the name of folder

Where can I see the file in path getApplicationContext().getFilesDir()

I am using the below code to create a file in android as
File file = new File(getApplicationContext().getFilesDir(), "content.txt");
I want to open the file in my phone. I went to the path
Local storage/Android/data/<my package>/files/
However, I cannot see the file. My Android version is 5.0.1. What is happen? One more thing, Do you think the above way is a good way to save file? What is common path to save a file in Android? Thanks
The file locates in /data/data/[your package]/files,it's app private directory.
It's a good way or not depends on the purpose of your file.
if you want your file visable only to you(the app developer),the folder is right
if the file can be visable to others and the file is large,then you can put it to sdcard folder
if you want the file stay even if your app being removed,you shouldn't put it in this folder.Because when an app removed,the all data in /data/data/[package name]/ will be deleted also.
You find in:
/data/data/[your package name]/files/

When using emulators, how to interpret the output path when calling Context.getFilesDir()?

Inside onCreate() I have this line:
File aux = context.getFilesDir();
which outputs this:
/data/user/0/com.example.tirengarfio.myapplication/files
but.. where is this path exactly insde Linux filesystem? or is it taking as reference the Android Studio Project root directory? os should I create it somewhere?
EDIT:
As I said to #Simas, but by the moment Im not connecting any smartphone. Im just using emulators on Linux. My intention is just reading a file using
FileOutputStream fos = openFileInput(FILENAME, Context.MODE_PRIVATE);.
So, where should I place the file inside the Linux filesystem?
This is the path of your app's local data folder. It can either be in the memory card or the device storage.
There's no easy way to access it if your device is not rooted but here's a starter:How to access data/data folder in Android device?
Let us see what the doc says :
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
No permissions are required to read or write to the returned path, since this path is internal storage.
So actually the files you will find are those which were saved with the very same function openFileOutput(String,int) by your/other applications.
So basically if you want to test some functionality (which I suppose) write a unit test that uses this API openFileOutput(String,int) to store some mockup data and then get it again with Context.getFilesDir() and some code for file processing.

Writing to the internal private storage in Android

I am trying to save some data from my app in a simple text file on the internal private storage area (app user preferences). I have read through many questions on here (StackOverflow) and tried the solutions suggested with no success. The simplest solution, it seems, would be the one suggested here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal but I cannot get this to work on my test device. I have also tried to create the file using the methods available in the java.io.File with the appropriate methods. I have also tried to create the file on the SDCard with the same result, fail. I have tried many solutions listed in other answers, following the code and instructions suggested exactly and find the same result. I am beginning to feel that I am missing some important bit of code, or a setting flag somewhere, I have set the permission in the manifest file:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
To be clear, I am trying to write to the device's internal, private storage. It is a small file containing a name, phone number, and a couple of type int flags. What ever method I use, I either find that the file did not create (or change if I place the file manually on the SDCard), or I get a NullPointerException when I try to reference the file or file location:
private File fILE = new File("Mydata", main.FILENAME);
or
private File fILE = getDir("Mydata", 0);
I am running the code on a HTC Hero, updated with the latest service release from Sprint. Any help would be GREATLY appreciated, Thanks in advance!
-Steve
Update (2/2/11): Using a EVO (API 8) I still get a NullPointerException. The code generating the exception is below, any thoughts on why my app can't access the internal storage? I have this problem on three different physical devices using two API levels (API 7 and 8).
File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");
UPDATE 2: 2/4/11 - I have found that I cannot see the file structure on the physical device (data directory) under any circumstance. Any one have any thoughts on this? The device is properly configured and can run app from eclipse or adb.
UPDATE 3: (2/9/11) - I think I may have found what the problem is here, but I am not sure about how to deal with it. I have figured out that the permissions on the /data/ directory on the physical devices are: drwxrwx--x. I am not sure why it is this way, maybe something to with Sprint? I have found this set this way on an HTC Hero, Samsung Epic (Galaxy S), and HTC EVO all on Sprint. The issue appears to be that DDMS and my app do not have r/w access to the directory. I need to figure out 2 things here, why it is like this and how to over come this issue in the wild. Again, any help here would be AWESOME!!
UPDATE 4: I think last February was a total blonde moment for me (see UPDATE 3). The test devices that I have are not ROOTed and hence no access (DUH!). After all the updates that he SGS and the EVO 4G have gone through, the result is still the same. I am still working this problem and will try and get back here with an update soon (hopefully less than a year next time).
Try changing the following line:
File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");
to:
File newfile = new File(this.getFilesDir() + "/","sample.txt");
Not a direct answer to your question, but nevertheless: I noticed that you don't want to store tons of data in your file. Would it be a sufficient alternative to use the Shared Preferences instead?
And perhaps even more interesting: does the problem occur even when you write to the Shared Preferences file instead?
http://developer.android.com/guide/topics/data/data-storage.html#pref
A physical device's /data/ directory is only available to the root user. Since the only realistic way to get this access on a physical device is to root the device, DDMS file explorer cannot get into this branch of the directory tree.
As to why the app will not write there, likely the issue is in the fact that I have signed the app with debug keys (not a big *NIX expert, but what appears to be the case here from my personal research).
I was dealing with the same issue. Finally, I found that you really don't have to give all file paths in order to create a new file in internal storage. Just mention the file name and it will be created under your app's package folder at the device. I did exactly mentioned here
And it works perfectly. I would say avoid mentioning Full file path like : /data/... in order to create a file (you need write permissions to create a file in such a manner). Let Android framework do the job for creating a private file for your app.
The internal storage area is sort of private to the application so the user must have root access(rooted device) to create and load the files. If you have rooted device this is how to write a file to the internal storage:
// Create a file in the Internal Storage
String fileName = "MyFile";
String content = "hello world";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
This should instantly create a file called MyFile with the content hello world. The internal storage directory is usually found in a special location specified by our app’s package name. In my case it is /data/data/[package name] and the files created are stored in a directory called files in that directory.
As #bianca says, you're better not using a file path. But instead, use only the filename to create a File. Something like this:
public static void saveTextToFile(Context context, String filename, String content) {
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
And to get the file, you can use:
File file = new File(context.getFilesDir(), filename);
Read more: Saving Files.

Read a file out of an android application

I need to place a file on a specific folder, so that my application can read it. First i want to try to create a new file with a specific path, but anyway i try, on the android dev tool on eclipse, I have an IOException.
Do you know a way to create "helloworld.txt" - For example - on the virtual device?
Thanks.
PS: I tried "new File(Environment.get..., "helloworld.txt").mkdirs();", and stuffs like that
If you want to create a file in the sdcard, you should check if the sdcard is ready (more info here), apart from the fact you need this permission in the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Try not use hard-code the path of the sdcard, use instead the API for that.
On the other hand, if you want to create a private file on the phone storage you can use this methods:
FileOutputStream fos = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
You can not access system folder by using android app.You need to transfer the file from your system to Android Emulator's Virtual SD Card by using file explorer availble with ADT plugin for android in eclipse.
Open file explorer in eclipse and import the file to device.

Categories

Resources