views:

1594

answers:

5

How can I copy files to the file system (place) in android? How can I access it?

+1  A: 

You can use:

Environment.getExternalStorageDirectory()

That will give you /sdcard if your device has an sdcard, or something different in other cases. For example the Archos.

Macarse
I know this...but this is not what I asked.
A: 

You can probably hide your file in the sdcard by prefixing it with a dot (ex: .myfile)

Rorist
In this case, the user can attach the device to the computer and then see the files. Am I right?
If he browse the sdcard from a windows box yes. But if you really don't want the user to mess with your file, use assets or raw resource. The only drawback is that it's readonly.Maybe you can use a database instead ?
Rorist
+1  A: 

copy files from android to local

adb pull /data/data/com.example.sample ./sample/

copy file to android from local

adb push ./sample/ /sdcard/sample/
Raja
+1 You can also do this from the DDMS application if you prefer to click things, but that's just a GUI implementation of these commands.
Chris Thompson
I don't want to copy to the sdcard...I want to copy to the file system directory
i don't think you write to the android file system directory directly. its only possible programmatically from the application that you are working on. Each application is given a protected space inside the /data/data/myApp , so this way one application cannot write to another apps folder. Well i'm a little out of touch with android development at the moment so i may be wrong.
Raja
I want to do it programmatically. Do you know how to do it? How can I get the app folder path?
ok.. there's Context.openFileOutput() which will give you a FileOutputStream to write on. here's what you may need to explore: http://developer.android.com/guide/topics/data/data-storage.html
Raja
A: 

I'm not sure if this is what you're asking, but here's another interpretation of your question:

You can place files in the assets folder in the project on your development machine. They will be bundled into the apk file and then access them through the AssetManager class. Something like this from within a Service or Activity class:

AssetManager am = getAssets();
InputStream is = am.open("filename");

This will allow you to read the contents of those files. I don't believe you can write to them though. There are other options for if you need to read and write files in your application's storage sandbox. This is done by calling openFileOutput(). I should note though that the user can always access the files on the device through ddms but other applications won't be able to access files stored in your application's storage sandbox.

Edit Based on your comment, you probably want to use

OutputStream out = openFileOutput("outfile");

to write to a file from your code. This code must be called from within an Activity or Service (something that extends Context). This file will be stored on the phone's file system and not on the sdcard. These files will not be accessible to the average user. If users know how to enable usb debugging then they will be able to access them and there is really no way around this.

Chris Thompson
A: 

You can also use a simple file manager application such as AndExplorer: http://www.lysesoft.com/products/andexplorer/index.html

lysesoft