How can I copy files to the file system (place) in android? How can I access it?
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/
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.
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.
I took some time to research about android's system files earlier, but almost always with android x86, i found out that the /system folder inside android is read-only,
it is an image with format is SFS format, this is read-only image format of linux so i don't think you can copy some thing inside this folder. I not sure about rooted android. Hope it will help you.
You can probably hide your file in the sdcard by prefixing it with a dot (ex: .myfile)
Related
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.
So I have some files I want my Android App to access, read and write.
I want to store it internally. Where can I put these files in my Java Project so they are accessible or can this not be done?
There are three ways to achieve this, and according to your requirements select the approch
on SDCARD
This is the normal SDCARD/in-build SDCARD in newer smart phones. you need to create specific folder structure and put your files there, here you can do file read and write both
but this in insecure because accessible to all the application
on Internal Storage
This is given as Applicaiton specific storage where you can create the file and do the operation, this is most secure way to do it, but this is generated run time so you can not push the files directly, you can put your files in RAW or ASSETS and copy that here
RAW and ASSETS
This is in the code structure only and only read access is given to this folder, you can not change this file run time.
if you select any one of this approach then simple goggling will show you the sample code.
You can read or write files in the path of your internal storage as
data/data/package_name/files . I had already answered a similar question you can check it out.
I have a file named flash_image in my apps asset folder.
I am wanting to copy this to my apps private data folder, and make it executable (my app will be running with root permissions, if this helps).
So, I want the file to be located at /data/data/com.liamwli.flash/flash_image and have that file executable.
I have tried many methods I found online, but I cannot get it to work.
How can I go about doing this?
If you want to save some data in /data folder, so you really want to save the data in devices internal memory. Check this out: Android Developers Storage Options Link. Also try changing mode to Context.MODE_WORLD_READABLE. I hope this will lead you in some way.
I want to decouple data from code on my Android application, and I am not sure of the best way to do that.
For instance - with the Linux Mahjongg game you can add new tiles to the game by dropping a specially formatted file into a specific directory. The Mahjongg game checks that directory when it starts up.
I want to do the same thing with my Android app - I want to be able to install the app, and then have separate installs for different data files. It's the data file installs that have me hung up. I do not want to have to set up my own server and write my own download code.
You can ship the data with the installer app, then use Input/Output Streams to copy the data from the assets or raw dirs.
Check this out:
Ship an application with a database
The answer has an implementation of in/outputstream. You don't need to use a db, just copy the file to ext storage.
One important detail: if you put the file in assets, it will be shipped compressed, and the phone/tab will try to uncompress the file in its entirety in memory. One (hocky) way to avoid that is to name the file .mp3. Assets in .mp3 format are not compressed. (Hey! I said it was hocky!)
The installer app can either uninstall itself by using ACTION_DELETE in an intent (see http://android.amberfog.com/?p=98 for details) or just show a msg to the user that it's safe to delete the data app.
HTH,
llappall
by dropping a specially formatted file into a specific directory
You can do that on external storage. Create a directory, and check it when your app starts up for new files. Tell the user they have to stick the magic files in the magic directory for it to work.
I am creating android application which contains DB that needs to be hidden(not able to access by the user)in the SD Card. Can anyone tell me how to do this?
Any file stored on the SD card is accessible both by applications running on the phone, and by users who have mounted the SD card (both while it's in the phone and otherwise).
You can change the file properties to make it 'hidden', but it will still be easily found. There is no way to make a file on a public partition like an SD Card 'secure' in the manner you describe - users will always be able to copy, delete, and potentially change the file.
The best solution to your problem is to look into ways to encrypt your database to record it securely. You won't be able to prevent users from deleting or copying the file, but you should be able to make it difficult for them to read data from it or modify its contents.
For an easy solution, just prefix the db file with a . (like .dbfile). This will hide the file on Linux based systems, including android.
I am assuming that you want to hide the file to prevent users from accidentally deleting/changing it.
SD cards cannot be secured in the fashion you seek.
just name the file start by ."Dot"... ".myFolder/.myfile"
You can create a folder whose name starts with a dot(.) example (.dbfiles) android See these folders as configuration folder and do not show content of these folder in applications like gallery,music player...You can see these folders with advance file explorer like Es File explorer
Sdcard you cant't! Internal app data partition is the place for you.