How can I access music files in the device into my application programmatically?
Another approach could be to use the MediaStore content provider to find your music: http://developer.android.com/reference/android/provider/MediaStore.html.
There's a DATA field in the MediaColumns that holds the data stream for the media file.
Google has an example of reading images from the MediaStore a bit more than half way down this page: http://developer.android.com/guide/topics/providers/content-providers.html. You should be able to adopt the code to read music without too much trouble, I think.
EDIT: Google has re-written the documentation on content providers. The example I referred to above doesn't seem to exist any longer. But once you have an URI for your media, you should be able to get an Input stream by using:
InputStream is = getContentResolver().openInputStream(uri);
Depending on where your files are stored you can use:
Environment.getExternalStorageDirectory();
or:
Environment.getDataDirectory();
Both will give you the root path to their respective data. i.e. getExternalStorageDirectory() will gives me /mnt/sdcard/ on my Evo 4G since my SD card is registered as external storage for the device.
After that you have to know where your files are and make your way to that folder.
Related
i still don't understand the change made on access media files in android Q. My app downloads videos, but playing or retrieving the video gives error.
Noticed, for android Q, every media file outside of app specific folders need to be registered in the contentresolver. In which the content resolver gives a special id to that file in return. Am i right?
if so how do i register a media file in content resolver and at the same time get the uri of the media file
A good explanation on accessing, saving and opening media files on android Q will be appreciated for a better understanding
The file manipulation changes from android 9 (Q) are really confusing.
Have you read the basic documentation on data and file storage?
Basically consider your app can write and read files that it created inside internal storage or primary external storage on the folders that belongs to this app.
Reading from sdcard can be done but requires permission from the user.
Writing on sdcard can be done with limitations and is not so easy. It also requires user permission.
Media files can be handled by MediaStore api and if they are inside the folders from your app it is easier.
If you want to store Media file on other folders different from the default media folders, then it is a little more difficult.
Please, define better what are your app flows and requirements in order to be easier for someone to help.
I'm here speaking in context of USB not a Memory card or Internel memory and need a exact path not a Uri.
Till Android L i get a File path from a /mnt or /storage or any other directory.but in marshmallow i didn't find where my usb is exactly.
then i move forward towards the Storage Access Framework.but it's not returning the actual path it's return me a Uri.
I also go through the AFileChooser Project and A stackoverflow post
Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT
but It's not helpfull.So is there any way i can access the Realpath like (/storage/abc/filename.mp4).
then i move forward towards the Storage Access Framework.but it's not returning the actual path it's return me a Uri.
That is because the Storage Access Framework does not work with files. It works with storage providers. Storage providers are welcome to store their pieces of content however their developers want. That can include content stored on servers, content stored in databases, content stored in encrypted containers, content generated on the fly, and so on. None of those have an "actual path" that is meaningful to you. And, if you are using the Storage Access Framework, the user can choose whatever storage provider that the user wants.
So is there any way i can access the Realpath like (/storage/abc/filename.mp4).
Make your own copy of the content, using the Uri and Java I/O. Then you know the "Realpath" of your copy.
A Uri is like a URL. There is no requirement for the URL of this Web page to map to some file on your computer. There is no magic algorithm that will convert a URL to a local file path... other than to download the content yourself and create a local file.
I used list.getPlayList(Environment.getExternalStorageDirectory() to get songs from sdcard, but unfortunately if phone has both "Internal phone memory and external sd card" it reads file only from phone memory,but I want from both to be added into single list to get the Mp3 files.
If there is any code, please provide or any links. Thanks in advance.
There is no direct filesystem access to removable storage on most Android devices.
AFAIK, most media players do not work with the filesystem anyway. They use MediaStore, which contains an index of all files reachable by the system, including those on removable storage. You can use the Uri values that you get from MediaStore with MediaPlayer via one of its create() methods.
Is there any way to get the absolute path from specific Folders & Files located in the removable SD-card, using the Storage Access Framework (SAF)?
Thanks
No, and even if you could, you would not be able to access them, unless they happened to be in one of the couple of locations on removable media for which you have read access (e.g., getExternalFilesDirs()).
The Storage Access Framework returns Uri values pointing to documents. Use ContentResolver and methods like openInputStream() to work with them. This is roughly equivalent to getting URL values pointing to documents and using an HTTP client to open a stream to work with the contents of those documents.
I have images uploaded on the simulated sdcard on my emulator. I downloaded them from the browser, using the long-click on each image.
When I look at the file explorer in the DDMS perspective, I see that they are in the directory sdcard/download.
Now if I want to download some audio files and use them in an app to, say list the title of all audio files, will they go in the same directory? (I am trying to push the audio files manually from my computer to the emulator).
This doesn't seem right. Shouldn't the media files of different types (pictures, audio, video) go into different directories?
Also, thinking this through a little, how is something like this related to the physical directory on the sdcard?
Uri mMedia = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Thanks!
The correct way to access media content is via the media content providers on the device.
Content providers make it easy to access data in a unified way, whether that data is stored on disk (i.e. /sdcard/...) or in something like a SQLite database. Content providers generally expose data using a custom Android-specific content:// URI scheme.
To query content living at a given content URI (such as MediaStore.Images.Media.EXTERNAL_CONTENT_URI), use the ContentResolver class. Examples on how to use this class can be found all throughout the SDK samples and Android Open Source Project (gitweb).
Also look at the MediaScannerConnection class for information on triggering media scanning (which populates the media content providers), etc.