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");
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.
On my Android device, I have multiple files located in my 'external storage' directory, but not all files are listed when using 'listFiles' in my Java Andoid app. Only files with certain extensions are listed. I want to list all files.
Example: When I have two files named 'Testfile1.mp4' and 'Testfile2.txt', then only 'Testfile1.mp4' is listed. If I rename 'Testfile2.txt' to 'Testfile2.mp4', then both files are listed, but I want to list all files without renaming.
Here is the code I use:
File path = Environment.getExternalStorageDirectory();
File[] files = path.listFiles(file -> !file.isDirectory());
In 'AndroidManifest.xml', I have:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and in my app, the permissions are confirmed by the user of the app.
I tried with Android Studio, with an empty Activity, with 'minSdkVersion' set to 28 and 'compileSdkVersion' and 'targetSdkVersion' set to 30.
I'm trying to read from a file in Android Studio, in a small Java app. So I'm trying this:
File test = new File("C:\\testing\\testFile.dat");
if (test.exists()) {
System.out.println("test exists");
}
else {
System.out.println("test doesn't exist");
}
The file definitely exists, but it keeps on reporting that the file doesn't exist. I was able to work around this with another file by using the AssetManager and reading it through a stream, but the method I'm calling now requires a File's absolute path, but it's point blank refusing to find the file.
Am I doing something dumb, or misunderstanding something?
UPDATE
Ok, thanks for the input, I've now solved the problem. First I had to upload the file I wanted into the virtual device's storage, then I was able to get the path to it.
File test = new File(this.getFilesDir().getAbsolutePath(), "testFile.dat");
but the method I'm calling now requires a File's absolute path
Assets are files on your development machine. They are not files on the device.
Ideally, you switch to some library that supports InputStream or similar options, rather than requiring a filesystem path. If that is not an option, you can always get the InputStream from AssetManager and use that to make a copy of the data in some file that you control (e.g., in getCacheDir()). You can then pass the path to that file to this method.
You can place this file in your assets/ folder inside the android project and access using the following code.
val inputSteam = assets.open("testFile.dat")
or place it inside the res/raw folder and access it like below.
val inputStream = resources.openRawResource(R.raw.testFile)
We can't access a file on a development machine like this and won't be available on an android device so it will break so it's better if we move this somewhere inside the project and access it as above.
There is a file strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<string name="param">0</string>
need to read a variable named "param" and change it to another int variable
or read and write to a file located in this APK, for example /assets/config.txt?
Don't try to modify the APK.
You should consider using other "persistent storage" options for your Android application ... you don't really want to be modifying the contents of your APK on the device. Shared Preferences, External Storage or SQLite Databases are among the options you have. You may want to read more documentation about these options on the Android developer area here
You cannot write/modify file in assets folder runtime.
Please check How to write files to assets folder or raw folder in android?
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