I have an Android application that generates some data (simple text file) that I would like to transfer to my PC for further processing. My understanding is that my storage options for a place to save such a file are a) internal storage or b) external storage. My device is a rooted Nexus 7 running Marshmallow, and I can't get either option to work.
With internal storage I'm able to write the file but then it's nowhere to be found using ADB or Eclipse DDMS. With external storage I'm getting FileNotFoundExceptions which I'm guessing are due to new complicated permissions, so none of Android saving file to external storage is working.
Is this possible and, if so, is there an easy way to do it?
What is your targetSdkVersion? If you target 22 and below, the new permission policy won't take effect.
It is possible to write a text file in Android and read the same in your PC using external-storage.
This app does the same.
You can find a simple example here.
If this does not work for you, kindly share your code-snippet that writes the file.
Related
I'm trying to create an android application that writes to a text file that can later be accessed once a button is pushed.
The past week i've tried a bunch of methods that people suggest to write to the internal storage, and sometimes it appears to work (using an outputwriter, and also a File class?), but i'm never able to locate the file on the Android device I test-run it on.
I'm rather new to development for Android, so all this is confusing to me.
Thanks
If by "internal storage" you mean what the Android SDK refers to as internal storage, this is not possible. Files that you create there are only accessible to your app, not by file managers on or off the device.
If by "internal storage", you mean what the Android SDK refers to as external storage, you need to:
Get a File pointing to a directory on external storage, such as calling getExternalFilesDir() on some Context, like your Activity
Create that directory if it does not exist
Create a File object pointing to the file you want to create, off of that directory
Use standard Java file I/O to write to the location identified by that File
Use MediaScannerConnection and its scanFile() method to tell Android "hey, I just put a file on external storage, please index it so it shows up in file managers"
Also:
Ideally, you do the disk I/O on a background thread, so you do not freeze the UI while that work is going on.
Depending on your minSdkVersion and where you choose to write the file, you may need the WRITE_EXTERNAL_STORAGE permission
Depending on your targetSdkVersion, you may need to ask for WRITE_EXTERNAL_STORAGE at runtime
You CAN access internal storage sometimes, but this depends on your phone.
You always can get data from internal memory for rooted phones (need root).
Files are in the folder /Android/data/
Some vendors allows you to run root shell on non-rooted phone through adb
(I saw this behaviour on Explay tabet) just run adb shell su to
test. Then you can copy your file from internal storage to public
with shell commands.
adb pull may also work in this case. (Again
vendor dependent)
I am trying out a test application on an Android emulator. I would like the app to write data to a file that is stored on my C drive. I have read and written data off Samba drives and webservices, but reading and writing to my local hard seems to be a puzzle. I tried filePath = "file:///C:/test/input/"; to point it as my parent folder, but the Emulator doesn't seem to recognize the directory, and I get a 'not found' error.
Any ideas will be appreciated. Thanks.
I would like the app to write data to a file that is stored on my C drive.
That is not possible, unless that file is accessible via some server. The Android SDK emulator does not have access to the filesystem of the developer PC.
Given that you are using an android emulator I am guessing it is setting up some special folder to act as a proxy for the androids hard drive. Meaning that the android emulator doesn't understand what your C drive is and instead is looking in it's 'hard drive' for a file. Find out where the emulator puts files for android, it could be a temp folder, and store your file there.
Edit: Where is Android Emulator Internal Storage this question should help you find where the files should be stored and the naming conventions.
If you don't care using genymotion emulator instead you can refer to this answer, pointing to this post, it has just what you need
Recently I'm in trouble about creating file in Android SD card with special character from iPhone.
My device : Galaxy Note4 KitKat.
My PC : Window7
I got a file from iPhone to my PC.
This file contains an emoticon (http://chars.suikawiki.org/string?s=%F0%9F%90%8D)
I could transfer this file to internal SDcard from my PC but I could not transfer to removable SDcard.
Even I can't transfer copied file from internal to removable, too.( I used FileOutputStream, File.createNewFile to copy)
I suspected SDcard's file system and tried to other FAT16(2GB sandisk Sdcard ), exFAT(64GB samsung SDcard).
But I failed both.
I tried to found file system spec but i found nothing related.
I also suspected reserved file title characters, but it was not related.
In short,
Why cannot transfer this file to removable SDcard?
How to copy programmatically this file to SDcard without renaming ?
===== add snippet =====
Below special character seems emoticon in Android device file browser but in my PC, it looks ordinal special character.
new File("/storage/emulated/0/🐍🐢🐙🐌.txt").createNewFile(); //internal storage. work.
new File("/storage/extSdCard/🐍🐢🐙🐌.txt").createNewFile();//removable storage. not work
I stumbled onto a similar issue with saving to SD. It appears to be a security issue. You now need to use the Storage Access Framework See Here.
You could use the files allocated to your Application though and not have to use the Storage Access Framework. One way to access your Applications data directory is by either using:
ContextCompat.getExternalFilesDirs(context, type)
Which would be backwards compatible. Or you could use the following for Lollipop Users
Context.getExternalMediaDirs()
Each of this method will give you 2 Directories. 1 for internal memory and other for SD (If device has slot). From here you can save all your application specific data.
BE WARNED: this data will be deleted if user ever uninstalls your application.
Hope this helped!
I am working on a project that needs to store some .txt to android, and get it from the computer for other use. From what I read from the documentation, I know that there are two types of storage: 1 Internal, which is somewhere deep in the phone that is private to the app. 2 External, which includes the SD card and the Internal Storage of the phone. I want to store it to External->Internal, and I am using this line of code to do that:
public String WalkDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/Walks/";
When I logged WalkDir, LogCat says "/storage/emulated/0", I stopped the app, checked with the adb shell, and there is no folder "0" but "legacy". I unplugged the phone and plugged it back in, the "Walks" folder is now in the root directory, and I don't need adb shell to access it.
So my question is, can you help explain how this system works? Why did "0" disappear? What is "legacy"? Why is the file in Internal Storage when I unplug&plug it?
Thank you very much!
Those are what in Linux are called symlinks (like shortcuts in Windows) that various system apps in Android are using..
/storage, /sdcard are sym linked folders,that means when you open one of those, it redirects to the original(/data/media/0), as for the 0 is just a multi user feature implemented in android 4.2, but only enabled on tablet androids.
Why do this Sym-Link?: simple so it dosent break apps(not only file explorer type of app, but all apps).
Still dont get, why it would break?.Simple. android api have lots of ways to write/read files from folders, u can do manually,u can get the data path, u can get the sdcard path, etc,etc. so to not break that they just does these sym links, thats why in one app the storage contentents are listed on /sdcard but on others, is /storage, etc,etc. one example of an app that broke because of these changes to android is titanium backup, u need to change the internal storage on it, so it work.
2 mount points pointing to the same storage device and partition.
If you create something in one folder, it will show up in the other. Same applies for deleting stuff.
They do not take away more storage space, as it is only available once but shown twice.
You also don't need to worry about it in any way because file browsers normally set their default directory to one of these locations.
As far as i know, Google changed the mount points in Android 4.2 to /storage/emulated/0/ due to them switching to MTP and EXT4(?) for the sdcard. The other mount points are still there for compatibility.
I'm writing an Android app which uses wi-fi, so I can't easily debug to emulator (no wi-fi support... ;-), so I go with my real device (a Samsung Galaxy S).
I would like to be able to read data files my app writes, to debug and test.
If I use, say:
new File(getFilesDir(), "myfile.xml");
I get my data file written to /data/data/MYPACKAGE/files/, but that directory is non accessible via adb (nor via Eclipse's DDMS).
My device is not rooted (and I'd prefer to avoid rooting it, if possible... ;-)
Where should I write my data file to?
It probably makes sense to put the files on the sdcard during development, formally you should call getExternalStorageDirectory() to find it and of course will need external storage permission.
Alternatively, you could give public access to your private files in the debug version; just don't forget to turn that off before you ship (as a certain Internet telephony company reportedly did). However, this will not make the private files browsable as the intervening directories are not, you would only be able to adb pull them via their exact path name.
A third choice would be to leave the data internal and private, but have a debug function to copy it over to the sdcard for analysis. You could even do this in a separate .apk establishing a shared user id with the first, meaning no changes at all to your application.
Simply use external storage!
You can write to your SDcard. You should use getExternalStorageDirectory() to get your SDcard's path. You will have to include the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your Manifest to do that.
The answer differs depending on your API level. Review the section in the documentation on external storage to get the answer for this.
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
For a somewhat generic answer, the sdcard directory that you should be storing files in is the directory returned from getExternalStorageDirectory() (which should be the root of your sdcard or possibly internal expanded storage as with my Captivate), with subdirectories of /Android/data/your.package.name/files
Oh yes, and as another poster mentioned, don't forget the WRITE_EXTERNAL_STORAGE permission in your manifest.