Get absolute path of a file [Android] - java

I have an app in which I write a file into the apps file folder(with a Fileoutputstream).
When i want to access it again I use a Bufferedreader with the location:
"data/data/com.mycomp.myapp.rstats/files/stats".
This works on pretty much all devices because the app gets installed there. But I imagine if somebody puts it on an external SD card, the path will be different. How can i get the absolute path of the file location?

You can use.. But do check for storage type exist before use it.
File extStore = Environment.getExternalStorageDirectory();

Related

Why is android unable of finding my file?

I want to access a file in included in the folder of the app and I want to be able to access it localy. I also don't want to use methods like android.getresources() and just use
File file = new File( filePath );
My file is saved in app/src/main/assets/ball.obj
The problem is when I do File file = new File("app/src/main/assets/ball.obj");, it alwas gives me a FileNotFoundException. I also tried moving the file in other places and absolute pathes, but it still doesn't work. Please help.
My file is saved in app/src/main/assets/ball.obj
That is a relative path to a file on your development machine. Assets are not files on the filesystem of the device. You cannot use File to access an asset.
To get an InputStream on your asset, call getAssets() on your Activity or other Context, then call open() on the AssetManager returned by getAssets().
For your call to open(), you need to pass in the relative path within assets/ to the asset that you wish to read in. So, if you are in a method of your Activity, getAssets().open("ball.obj") will give you an InputStream that you can use to read in the contents of that particular asset.

How can I find the main directory on my Android app?

I would like my Android app to find the main directory on my tablet's internal storage. The furthest I can get is reading the contents of the "user.dir", but that seems to be the root directory and the listing there is too immense to find what would be considered the DOS equivalent of C:\
I am listing the code snippet I use to read the directory to show I can access the internal storage, but somewhere I'm not able to do it correctly.
workingDir = System.getProperty("user.dir");
File f = new File(workingDir);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
Thanks
in fact, if you were to open the "My Files" explorer that comes with Android the top option on my tablet is called Internal Storage, and it lists all the viewable folders from within
That is what the Android SDK refers to as external storage. You can find the root of that via Environment.getExternalStorageDirectory(). However:
Don't put things directly there, just as well-written Windows programs haven't put stuff in C: for the past couple of decades. Use Environment.getExternalStoragePublicDirectory() for standard locations (e.g., downloads directory), or use getExternalFilesDir() on Context for a location that is unique to your app.
You need to hold READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE permissions, including setting up runtime permissions for use on Android 6.0+ devices.
You can try to give a look at the android Environment documentation and access internal storage root/main directory with this Environment.getRootDirectory()

android - retrieve all files from /res/raw folder as File type

How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)

Java - Audio URL

I have a audio file stored in a folder within the project and I'm trying to call the sound file within the folder.
I am able to call the file using the full URL
new java.net.URL("file:C:\NetBeansProjects\Puzzle\audio\applause2.wav"));
But is there a way to shorten it so that if I move the project I don't require changing the code?
Thanks
File file = new File("audio\applause2.wav");
Assuming you keep the audio folder at the same level relative to the jar

Android internal storage reading file

I have a problem with Android internal storage. I have created folder in package root folder calling getDir() and with MODE_WORLD_WRITEABLE because I want camera app to write captured image in this folder. Anyway, I can see that captured image is inside that folder with DDMS.
Problem is that I cannot read that file.
I tried to read file with this code:
File file = context.getDir("images", Context.MODE_WORLD_READABLE);
File image = new File(file, "image.jpeg");
if (image.canRead())
Log.w("read", "can read");
else
Log.w("read", "can't read");
And in LogCat there is only second message (can't read).
I have also tried to create FileInputStream with file name but I receive FileNotFoundException.
Can somebody tell me what I'm doing wrong here?
Thanks in advance
EDIT:
Reading file is correct but only problem is that when cam app is saving image to specified location, permission set by cam app to image file are -rwxrwx---. After changing permissions with
adb (chmod 777 image.jpeg)
I was able to read image. Interesting thing is that cam app is writing images files to sdcard with ----rwxr-x.
Is there any way to change file permission in runtime?
Why not put it in the default photo directory?
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
( From: http://developer.android.com/reference/android/os/Environment.html )
See also a more complete example invoking the camera app: http://developer.android.com/training/camera/photobasics.html
Referencing to Android developers you should use openFileOutput() and openFileInput() to work on the internal storage... you are not allowed / not able to make dirs there. Android does it itself.. Those data will be cleared when your application will be deleted.

Categories

Resources