I want to store data recorded with the accelerometer on the external storage but I have an error when I try to create the directory in which I want to save the data. My code is as follows :
dataDirectory=Environment.getDataDirectory();
path=DataDirectory.getAbsolutePath();
path+="/"+getResources().getString(R.string.DirectoryName);
myDirectory=new File(path);
myDirectory.createNewFile();
The createNewFile call triggers an IOException.
I can add two things : the path variable is set to "/data" after the call to getAbsolutePath and the getExternalStorageState function returns "mounted".
Can anyone tell me what is wrong in my code?
Thanks in advance for the time you will spend trying to help me.
You can't have permission to write in the /data directory
Use the sdcard directory
Or try working in the /data/data/com.yourapplication/files directory where you do have permission. Use Context.getFilesDir() to get the path of your application's working directory.
try this
path=DataDirectory.getAbsolutePath().toString();
add WRITE_EXTERNAL_STORAGE permission in your manifest file
Related
While working to upgrade to API 33 from API 28 I can no longer access the files that I have created with ACTION_CREATE_DOCUMENT intent without using its URI.
I have tried making sure to ensure READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions were taken before attempting to access the file. I manage to create the file but when creating its path and trying to access it at that path the app suddenly fails to open it even though it should be the owner of that file if I'm not wrong?
I absolutely cannot use MANAGE_EXTERNAL_STORAGE permission because that doesn't fit the usecase that I have.
Is there a way to use the path instead of the URI returned to write to the file? the file extension is csv.
I have created a sample to write and read the file.
Create the File:
Intent intent = new Intent(Intent.ActionCreateDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType("application/txt");
intent.PutExtra(Intent.ExtraTitle, "anew.txt");
StartActivity(intent);
Then I saved the file into the Download Folder by the system file manager and write the file.
File.WriteAllText("/storage/emulated/0/Download/anew.txt", "this is file content");
Every thing worked well after I granted the storage permission to my app. But when I saved the file in the Documents Folder, I will get the error about accessing path /storage/emulated/0/Documents/anew.txt is denied.
The read and write storage permission cann't access the Documentss Folder with a path.
I am trying to learn android programming. As a beginning, I am trying to understand the internal file system & accessing them. Any help is highly appreciated. Following are my challenges:
How to create a 'TEST' folder through the android emulator/Android device monitor? I need to create the folder where the alarms, download, DCIM folder are present.
What is the absolute directory path to the 'TEST' folder created? Is it possible to access the TEST folder using the code below:
List<File> list = Environment.getDataDirectory()+"/TEST".listFiles();
Does any permission to be mentioned in the manifest for read/write to internal storage?
PS: I use Android Studio.
You would want something along the lines of
File TEST = new File(Environment.getExternalStorageDirectory(), "TEST");
TEST.mkdir(); // make directory may want to check return value
String path = TEST.getAbsolutePath(); // get absolute path
and permissions
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
Further reference
Android Open External Storage directory(sdcard) for storing file
How can I get external SD card path for Android 4.0+?
https://docs.oracle.com/javase/7/docs/api/java/io/File.html
File file = new File("storage/emulated/0","yourfoldername/");
This worked for me if this doesn't help, then in use
File(Environment.getobbdir(),"yourfoldername");
Well, this is not exactly a question, as I'm not really "stuck" on my code, but I've found some strange behavior for Android API regarding accessing the external storage and the File.createTempFile() method, and I'd like to understand what's happening...
Please note that my manifest does not include the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">.
Part 1 :
I have the following code which does work as a charm :
File tempFile = new File(Environment.getExternalStorageDirectory(),
"my_temp_file.png");
it creates a temporary file for me, and I can write datas in it without any trouble.
Question 1 : Why does it work, as I'm not supposed to have writing rights on my SDCard ?
Part 2 :
I've tried to change my code to use the createTempFile which is the official method to create temporary file. So I've tried :
File tempFile = File.createTempFile("my_temp", "png",
Environment.getExternalStorageDirectory());
and added the WRITE_EXTERNAL_STORAGE in my manifest.xml. Guess what ? This does not work, and I get a java.io.IOException :
09-07 14:07:29.061: E/_CLOG(19982): java.io.IOException: Permission denied
09-07 14:07:29.061: E/_CLOG(19982): at java.io.File.createNewFileImpl(Native Method)
09-07 14:07:29.061: E/_CLOG(19982): at java.io.File.createNewFile(File.java:1257)
09-07 14:07:29.061: E/_CLOG(19982): at java.io.File.createTempFile(File.java:1322)
09-07 14:07:29.061: E/_CLOG(19982): at com.(...).onClick(ProfileImageUpdater.java:58)
Question 2 : Why this doesn't work, whereas imho it should ?
try like this...
File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputFile = File.createTempFile("prefix", "extension", outputDir);
Edit :
for Question 2
It may be issue as below link...
Environment.getExternalStorageDirectory does not return the path to the removable storage
you're getting this error because you declare on the manifest.xml permission to write to the external storage. you need to add this permission.
also,
do never ever ever ever write files (specially temporary ones) directly on the getExternalStorage().
This will put directly on the SD-card and will create a mess.
For temporary files that is only to be seen by your own application you should use:
getExternalStorage() + "/Android/data/<package_name>/cache/"
replacing by your app packaged, e.g. com. Orabig.myFirstApp
that special folder is automatically deleted from the system if the user uninstall the application, keeping the system free from temporary files.
edit:
Please note that my manifest does not include the
you have to!
edit:
also, if you creating temporary media files (PNG for example) is good practice to create an empty file named .nomedia on that folder. That way you avoid the Media Scanner scanning it and showing it on the gallery.
last edit:
and before creating files you must create the folder by calling mkdirs() on the File object.
I finally figured out what was hapenning...
Question 1 : In fact, the file that was created was meant to be used by the system application to retrieve and crop an image from the user's gallery. So my app didn't have to own the WRITE_EXTERNAL_STORAGE permission, as the file was written by the system itself.
Question 2 : I'm not sure. I guess that the Android do not allow to create a temporary file where I was trying to (as it's not the right place for it)
After searching 1 hour i do not found any solution to my problem.
I want to move a file from sdcard to assets folder and also overwrite the existing file in assets folder (both file are sqlite database have same name with just a little difference in data )
??
Actually, The behaviour of the android .apk file is read only so the directory structure which are in that apk file follows the same, So no one can write this files or make changes on that at runtime,that's why You can't make a changes in /asset folder at runtime. you can read file from it (means copy files which are available in asset to any internal storage or /sdcard but not write it to vice-versa)
Its apply on all the directories which are build in .apk files. As per docs.
Assets folder are not Writable it is only readable so you cant copy from sdcard to assets, yes you can copy from assets to sdcard
If you want other apps not to access your database then what you can do is
Download the database from website. create a temp database also in app directory.
getDatabasePath() this will give you the path of the dummy database you have created now replace this with the one you downloaded.
PS.: User and apps having root access still able to access it and you cant do anything in that scenario
I have to create a multiple directory like this path("images/wallpaper/flower"). It should be stored in the External Memory of the phone.
How to do it? Any Idea?
Just get the SDcard path using Environment.getExternalStorageDirectory(); and then create the dir structure with the Java's File class.
You should know that the sdcard can be unmounted so check it's status first.