Getting absolute path on android - android

I've got c++ code in which I try to get a file from a directory on an android device. I've tried different ways to set the path which I pass to the fopen() function like:
/Android/data/com.myapp/files/Blip.wav
There actually is this file. But I guess that this is not a proper way to write a path. (The example was obtained by the java code )
getContext().getApplicationContext().getFilesDir().getPath() + "/Blip.wav"

There actually is this file
Since I have never seen an Android device with an /Android directory, that is unlikely.
What would fit is if you are looking at /Android/data/com.myapp/files/Blip.wav in a desktop file manager, using a USB or similar connection. In that case, Android/data/com.myapp/files/Blip.wav is a relative path in external storage. Specifically, it maps to:
new File(getContext().getExternalFilesDir(), "Blip.wav")

Try using this.
File root=Environment.getExternalStorageDirectory();
File file=new File(root,"/PersonData/Blip.wav");
Here personData is the name of folder

Related

Path of a file in an internal directory

I have a file gpac.xlsx in the internal storage of my phone. I want to get its path. In android, it's confusing. Which one of these is the correct path?
/data/sdcard0/gpac.xlsx
/data/emulated/0/gpac.xlsx
Or some other besides these?
I have a file gpac.xlsx in the internal storage of my phone.
Based on your sample paths, your file is in what the Android SDK refers to as external storage.
I want to get its path.
new File(Environment.getExternalStorageDirectory(), "gpac.xlsx");
Which one of these is the correct path?
It could be either of those or something else. Use the Java snippet shown above to derive the proper path at runtime for the particular device that your code happens to be running on.

android - how to get path of mp3 stored in raw folder

After searching for solution, I have found from this link that it is possible to retrieve path like:
Using Resource Id
Syntax : android.resource://[package]/[resource_id]
Example : Uri.parse("android.resource://com.my.package/" + R.raw.mp3filename);
Which is exactly what I want. Problem is that, I'm using lolipop, and it does not work. When the package/resource is parsed, path from Uri.parse will be null. Is there other way around to write this, since in my Song class, I have only access to resourceId (R.raw.mp3filename).
Is it possible to get file path by using R.raw.mp3file
No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.
Either:
Do whatever you are trying to do some other way that does not involve a file path, 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 that resource to a local file, such as on internal storage

When using emulators, how to interpret the output path when calling Context.getFilesDir()?

Inside onCreate() I have this line:
File aux = context.getFilesDir();
which outputs this:
/data/user/0/com.example.tirengarfio.myapplication/files
but.. where is this path exactly insde Linux filesystem? or is it taking as reference the Android Studio Project root directory? os should I create it somewhere?
EDIT:
As I said to #Simas, but by the moment Im not connecting any smartphone. Im just using emulators on Linux. My intention is just reading a file using
FileOutputStream fos = openFileInput(FILENAME, Context.MODE_PRIVATE);.
So, where should I place the file inside the Linux filesystem?
This is the path of your app's local data folder. It can either be in the memory card or the device storage.
There's no easy way to access it if your device is not rooted but here's a starter:How to access data/data folder in Android device?
Let us see what the doc says :
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
No permissions are required to read or write to the returned path, since this path is internal storage.
So actually the files you will find are those which were saved with the very same function openFileOutput(String,int) by your/other applications.
So basically if you want to test some functionality (which I suppose) write a unit test that uses this API openFileOutput(String,int) to store some mockup data and then get it again with Context.getFilesDir() and some code for file processing.

DownloadManager directly into external storage directory

I'm trying to download a big zip file directly inside the external storage directory, for example
"/mnt/sdcard/Android/data/com.vexdev.audioguida.app/files/data"
i guess i should be using
DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)
but i don't know how to provide it with a path that is consistent across different android devices.
AND i'm also trying to get a path to access this file later, like this:
File file = new File(path + fileName);
i'm asking here because i know that those two methods are expecting different rooted paths, because i tried to provide a path like this:
Application.getAppContext().getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();
but the DownloadManager was not putting the files where the File constructor was searching it later. (It was downloading in the wrong directory actually!)
I'm looking for a way to download those files directly in the right directory, without having to move them.
i guess i should be using DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)
Not for the location you suggested. The closer match would be setDestinationInExternalFilesDir().
but i don't know how to provide it with a path that is consistent across different android devices.
There isn't even a path that will be consistent within one device, as different accounts will use different paths. For example, the path you typed into your question might be used on some devices for some accounts, but the details will vary.
i'm also trying to get a path to access this file later
That is covered in the DownloadManager.Request documentation:
setDestinationInExternalFilesDir() maps to getExternalFilesDir() on Context
setDestinationInExternalPublicDir() maps to getExternalStoragePublicDirectory() on Environment
i tried to provide a path like this
That is not how you use getExternalFilesDir(). Please read the JavaDocs to see what valid values are for the parameter to that method. getDataDirectory() is not a valid value.

how to cocos2dx read file on android

I have a text file "test.txt" under the Resources, and a function "parseSth(char* str)" in the "util.cpp" to read the "test.txt" do something.
However, if I use like this parseSth("text.txt"), the ios and android all cannot find the file. And use parseSth(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("text.txt")) will work fine on ios, but cannot find the path.
Why? How I can do?
I suppose that your file is embedded into APK file on Android and thus doesn't have a path to it per-se.
Use the following function to extract data from the file (it automatically handles apk/sd card differences).
FileUtils::getInstance()->getDataFromFile(filename)

Categories

Resources