I am writing a file to Environment.getExternalStorageDirectory + File.separator + "myFile.txt" with the permission to write to the external storage. This seems to be successful (i.e.: no error messages in the logcat).
However, my issue is when I try to access the data folders or a folder which will contain the files I am making via adb shell or otherwise I am told permission denied. I realise this is because I am not root and the device is not rooted. However, is there a location on my Nexus 4 where I am able to freely create, store and access files easily without special permissions? If not is there a simple way to solve this issue?
Thank you for your help.
Environment.getExternalStorageDirectory() does not point to /data but to /mnt/sdcard or /sdcard. All in all you are looking in the wrong place
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 have created a file in the below path using my applcation.
/data/data/VisionEPODErrorLog/ErrorLog.txt
I am able to see the file using Eclipse DDMS->File Explore ->Data
But when I installed my applcation in the mob device I am not getting that path in the device.
I need to see the exact file in the device
Is there any third party applcation needed to access the path.If so whats that or is there any way else to get that file from the device.if anyone konws please let me konw
please use below method
BufferedInputStream in = new BufferedInputStream(Mtx.getContext().openFileInput("ErrorLog.txt"));
i used in my application to read file stored under /data/data/ directory
i think this will help
/data/data/VisionEPODErrorLog/ErrorLog.txt
That is an exceptionally bad idea. Please do not do that. Please use getFilesDir() to get the root of your internal storage for files.
But when I installed my applcation in the mob device I am not getting that path in the device.
You should not be able to write there on a production device. Please do not invent random paths. Please use getFilesDir() to get the root of your internal storage for files.
You certainly cannot view anything in /data/data on a production device via any tool, unless you root your phone.
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.
When starting my android application, I need to create a directory on the sd card, for a small number of users this fails and I can't figure out the reason for it...
(I've found similar problems caused by the WRITE_EXTERNAL_STORAGE permission missing, it's there and it works for almost all users so I don't think this is reason)
I've simplified the previous situation to make it easier to explain, if creating a directoy fails, I run a test case where I try to make a .test directory on the sdcard:
new File(Environment.getExternalStorageDirectory(), ".test").mkdir() -> false
new File(Environment.getExternalStorageDirectory(), ".test").mkdirs() -> false
File properties of the relevant directories:
/sdcard/.test (exists=false canWrite=false canRead=false canExecute=err isDirectory=false isFile=false)
/sdcard (exists=true canWrite=true canRead=true canExecute=err isDirectory=true isFile=false)
/ (exists=true canWrite=false canRead=true canExecute=err isDirectory=true isFile=false)
getExternalStorageState=mounted
(canExecute returns err because the test is run on sdk < 9)
Suggestions and ideas are very welcome...
It is common when you don't have a permission in your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
As for me it was the only wrong thing.
I had a simillar problem and spent several hours to realise what is wrong. It didn't work on Sumsung Mega, but for other phones it worked fine.
I really had WRITE_EXTERNAL_STORAGE permission and getExternalStorageDirectory is mounted and available. But still the directory wasn't created.
What helped ?
I just restarted the divice! And it helped!
It's a pity that nobody adviced this before. Hope this will help!
First of all, technically mkdirs() returning false doesn't mean that it failed, it just meant that it didn't make the directories.
If the directories already exist then mkdirs() will continue to return false.
Secondly, in Android 6, not only do you need:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
You also need some equivalent of:
ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
For more info on checking permissions, see:
https://github.com/googlesamples/android-RuntimePermissions
I have had issues trying to create directories on the SDCard as well. I found what was happening is that my code would work fine on some phones, and also the emulator. However other phones would report errors.
I believe there is an issue with certain phones, the Atrix and the Bionic are two examples.
These phones when using the Environment.getExternalStorageDirectory() code actually return /mnt/sdcard.
However on these phones the SD card is actually /mnt/sdcard-ext.
So somewhere along the lines it looks like some manufactures have broken the getExternalStorageDirectory code, and somehow its returning a false location.
I had a heck of a time figuring this out because my code worked on many different devices, and still some users reported it failed.
My problem was that I was using mkdir for creating a multiple directories path and I got false everytime. Once I used mkdirs my problem got resolved
I had the same problem. The permission was set and also the path was the default one (/mnt/sdcard) but my android was mounted as an external storage device for quick browsing the SD card.
However this interferes with other apps.
I figured this out when diving into the adb shell:
$ cd /mnt
$ cd sdcard
cd: can't cd to sdcard
$ ls -l
d--------- system system 2011-12-07 01:59 sdcard
after unmounting the image I've got:
$ ls -l
drwxrwxr-x system sdcard_rw 2011-12-07 11:46 sdcard
As you can see while mounted the sdcard is in the system user group and there are no permissions at all. After unmounting the group is changed to sdcard_rw and permissions to read and write are granted.
I know this is an old posting but I thought I can still offer a suggestion. If you're running your app while developing (ie. the phone is plugged into the PC via USB), then chances are, the SD card has been mounted by the PC. You have to unmount it (you can do it from the phone or from the PC) in order to gain write permission, this requires that has been set in the manifest.
I thought I was having this problem while debugging an app. It was failing because the SD Card was mounted by the host computer while debugging. Unmounting from the computer made the app work again.
It is possible that these users have an SD card that is corrupt and thus mounted read-only. If possible, you should check with them to see if anything else can write files to it. Given that you have the WRITE_EXTERNAL_STORAGE permission, you should have no trouble making modifications to the SD card (and the permissions of everything in the SD card is world read/write for such apps, so nothing can mess with file permissions to cause them trouble either).
I ran into this problem when the symlink /storage/emulated/0 was somehow missing. What I did was use the camera app to take a picture. I observed that the pictures details said it was within /storage/emulated/0, though that directory did not exist from the file explorer. Then I rebooted the phone. After reboot the directory showed up in file explorer.
I was having a similar problem. My camera app was working fine in all the android versions but Q (10).
The issue was that I using Environment.getExternalStorageDirectory(); and its deprecated in android Q (onwards). The solution was simple, using something that was supported in the latest version which will be context.getExternalFilesDir()
Here is how I get it working in all the devices:-
public static boolean createDirectory(Context context, String directoryPath) {
File folder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
folder = new File(context.getExternalFilesDir(null), directoryPath);
else
folder = new File(Environment.getExternalStorageState(), directoryPath);
if (!folder.exists()) {
return folder.mkdirs();
}
return true;
}
This can also happen if you target SDK 29 or greater(targetSdkVersion in build.gradle) due to the introduction of scoped storage.
If that is the case, either change your target SDK to 28 or lower or add: android:requestLegacyExternalStorage="true"
to your manifest, under Application key.
I experienced a similar problem when using mkdirs(), however because running the command:
mkdir one/two
fails on Linux, then the API (http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs()) subsequently fails too. I guess this means there is no way to use mkdirs on Android? My (probably rather hacky) work-around was to create each necessary directory separately:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
new File(extStorageDirectory + "/one/").mkdirs();
new File(extStorageDirectory + "/one/two/).mkdirs();
I ran into this issue on virtual device. For me the issue was that I had defined no external storage for the virtual device. Hopefully helps someone.
I am getting error like this "
error:parent Directory of a file not
writable"
in android.
I've included WRITE_EXTERNAL_STORAGE permission, but it's not working.
Having that permission doesn't allow you to write anywhere.
You should get the writable path using: Environment.getExternalStorageDirectory();.
Apart from having correct permissions and a writeable path, as was already mentioned by Macarse, you might also want to check if the SDcard mounted is writable (check the Permissions tab in the FileExplorer view it should be something like d---rwxr-x).
If not then remount the SDcard(there are adb commands to do this but are risky, I think).
My usual course of action is switching off the phone and removing and then reinserting the SDcard. Sometimes just switching off the phone helps.
I'm getting this error too, only I'm trying to write to internal storage. Any ideas on how to fix the error?
Never mind. I figured it out. Data has to be written to the file streams as bytes if saving to internal storage.