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

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

Related

Getting absolute path on 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

How can i insert a .bin created on desktop file into apk

I have code for creating an internal file, there is random algorithem that create the data stored in it and i want any app to have the same file with the same binary data in it.
so i need to make the file on my desktop and add it to internal files some how.
my question is what do you think is the best way to do it.
i thought to locate it in my project, read it, and write it to internal files.
the problem is, i dont know where to locate my file in android studio so that it will be included in the external files and then where to read it from.
thanks. =]
hope i made myself clear.
Put it in src/main/assets/.
You can then access your file with AssetManager and do whatever you want with it.
From the Android Developers website:
main/assets/
This is empty. You can use it to store raw asset files. Files that you
save here are compiled into an .apk file as-is, and the original
filename is preserved. You can navigate this directory in the same way
as a typical file system using URIs and read files as a stream of
bytes using the AssetManager. For example, this is a good location for
textures and game data.
You need to move that into the assets folder. From there you can refer to the file.

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.

Getting File object or URI object from android assets directory

I must get a File object or a Uri object from android assets directory, but I only know how to get inputStream object.
Is there any other way to do it?
You cannot get a File object to an asset, as it is not a file on the device. It is a file in your development machine, but it is merely an entry in an APK file on the device.
Some things, like WebView, can work with a file:///android_asset/... Uri, where ... is replaced by the relative path within assets/ of your project to the file of interest. For example, if your project has assets/index.html, file:///android_asset/index.html would point to that file. AFAIK, not everything can use this sort of Uri, and in particular things that need a URI (e.g., java.net) probably cannot use it.

Using a file located in either the raw or assets folder

Currently I am able to download a file off the internet and store on the SD card, then use the file from there. However that makes the file (with proprietary data) available to be seen. I would prefer to use the file from somewhere like raw or assets folder.
I will skip the downloading code, but my code to use the file is this
File myFile = new File (android.os.Environment.getExternalStorageDirectory() + "/folder/filename.xml");
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(myFile));
Android opens the file with the default application and all is good.
I have found similar Q/A's that revolve around using code like
Uri.parse("android.resource://com.projectname.testing/raw/filename");
and
InputStream ins = getResources().openRawResource(R.raw.filename);
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
I would like to solve my problem by simply accessing the file as a file. However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes. This sounds like a lot of extra work, especially since my code above does work for a file stored on the SD card.
Does anyone have any ideas?
Thanks
I would prefer to use the file from somewhere like raw or assets folder.
Note that these too can be "seen".
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
setData() does not take a File. It takes a Uri. Use Uri.parse() to parse other types of Uri values -- you already have this code shown above.
However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes.
It definitely will need to be world-readable. Also, not all apps support all schemes, so apps that support file:// or http:// might not support android.resource://.

Categories

Resources