Small problem, I am trying to save my PDF file in phone (LG G5 - no SD card), but file is never saved, is there a problem with file path? Using PdfBox-Android library. See picture from debugger - Debugger picture. The aim is to save pdf file to internal downloads folder. I tried 10x solutions, nothing is working. Please help.
The aim is to save pdf file to internal downloads folder
Use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), not getFilesDir(). Also:
You will need the WRITE_EXTERNAL_STORAGE permission
You will need to handle runtime permissions
You will need to get the file indexed by the MediaStore before it will show up
I think you are trying to run the code on M version. Please check for Write External Storage permission and other read and write permissions in manifest file. For Android M and above versions use the concept of runtime permissions.
Let, me know if it was helpful.
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 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
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.