Environment.getExternalStorageDirectory() gets files from phone memory - android

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.

Related

Preference of storage in android

I have created one app which creates one file on external memory, but when I install it on different devices the files are created in internal sdcard in some device and not created in external(Physical) sd card.
My question is that. How do we decide between the internal or external sdcard.
Which has more preference to store file by default in android?
I use the
Environment.getExternalStorageDirectory()+ "PolicyTaskfile"+"/filename.txt";
It gives external or internal sdcard path depending on the device.
The short answer is that you do not get to pick. It's up to the OEM to decide whether external storage is truly SD card or just an internal storage device (for example: eMMC). With KitKat there is a notion of primary and secondary external storage, but no easy API to determine which you are using or which is really removable media.

How can we restrict a mp3 file to be played in other application in Android

Requirement is application will download some mp3 files and that are stored in external storage. We need to restrict those files to be played in any other media player provided by android.
As I understand it you only want your mp3 files to be played by your own app. Correct?
If that's the case then you have a problem with external storage as it has 'public' access status. Any app can access any file anywhere in external storage.
The closest you can get to restricting media files in external storage is to create a file called .nomedia in the same directory. This will prevent the media scanner from adding the files to the media content provider so other apps won't see them by default. Anybody using a file manager app, however, will be able to find and play the files with any media player they choose.
If you really want to restrict the files to your own app then you need to save them to internal (private) storage. Even than, a phone which has been rooted will allow access to any app's private storage area so there's still no guarantee they'll be 100% restricted.

Record and playback a sound on Android without storing a temporary file

In my app I want to record a 2-second long sound and play it back to the user. All examples I've found so far require that the audio data for playback either comes from a file or from a URL.
Both MediaPlayer and SoundPool classes accept only files, file descriptors, resource id's or URLs as input and not just, say, a byte array.
Since one can only write files to the SD card and NOT internal storage (is this so?), the app would require that an SD card is mounted and writable. But my app should also work if no SD card is present.
Is there a way to achieve this?
Thanks
Yes, an App can write to internal storage (where it is installed). Every Installed app is provided a Data folder to write its internal files. Also there is a cache storage provided.
These methods of Context can get you these directories:
getCacheDir() : Returns the absolute path to the application specific cache directory on the filesystem (Note: System may delete cache if its low on storage)
getDir(String name, int mode) : Retrieve, creating if needed, a new directory in which the application can place its own custom data files.
Also there is a method for External storage: getExternalCacheDir() but is unreliable since external storage might not be always there.
Also, if you just need to write files in App's internal data directory, there is a very simple method:
openFileOutput (String name, int mode)
You can use
'getApplicationContext().getFilesDir().getAbsolutePath()'
to get the path of the directory where the app is installed.
One limitation is, 'getFilesDir()' gives you the path to the directory on the file system where files created with openFileOutput(String, int) are stored.

Android get all media files on the whole filesystem (SD card + internal stroage)

I want to get the list of all the media files in the whole filesystem i.e. from the external as well as internal storage. How can we do it? Can content providers be used to scan internal storage media? Or scannning through the whole file system for the desired extension like ".mp3" is good enough?
If you are only interested in audio media then you need the following content providers:
MediaStore.Audio.Media.INTERNAL_CONTENT_URI
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Android is scanning automatically the files and keeping content providers for audio, pictures and video (and the generic files) up to date.

Accessing music files into my application programmatically

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.

Categories

Resources