I would like to show a list of files in the Android phone.
For example, i would like my app to be able to list all images stored in the phone, i dont want a gallery view, just a simple list.
I would also like to be able to show all the audio files
any help would be appreciated
Thanks
I'm assuming you mean on external storage. Read up on Data Storage, which will tell you the correct way to go about navigating the file-system.
If you're using API Level 8 or
greater, use getExternalFilesDir() to
open a File that represents the
external storage directory where you
should save your files. This method
takes a type parameter that specifies
the type of subdirectory you want,
such as DIRECTORY_MUSIC and
DIRECTORY_RINGTONES (pass null to
receive the root of your application's
file directory). This method will
create the appropriate directory if
necessary. By specifying the type of
directory, you ensure that the
Android's media scanner will properly
categorize your files in the system
(for example, ringtones are identified
as ringtones and not music). If the
user uninstalls your application, this
directory and all its contents will be
deleted.
If you're using API Level 7 or lower,
use getExternalStorageDirectory(), to
open a File representing the root of
the external storage.
For standard/common media types such as Music, video and images in the internal memory, there are respective ContentProviders in the system that manage them. If you want to build list of such items you should consider querying respective ContentProviders (Google for MediaStore Provider).
However, if you want to build an exhaustive list of all the files in a particular directory, internal or external, you can use File#listFiles() API as was suggested by Fredley above.
Related
I am confused with the new app storage system in Android. I am not sure where my use case falls under and I need your help in telling me the right approach for this
My app captures images and generates pdf documents. Prior to Android 10, I used to store them in an app directory where the user can easily navigate to them through other files browsing app (like Files app on Samsung). In addition, these files can be accessed from within my app (so essentially read and write).
With the new storage, I am not sure how to accomplish the same thing. If I use the internal storage then user can't see them. If I use the media approach, well it seems it is only for Audio/video plus they will not be organized in a folder like I have them organized.
Am I missing something? How would I solve this problem?
Thank you
On an Android 11 device you can store your files in a subdirectory of the public Documents directory.
You can do that using classic File means or the media store or SAF.
Other apps can see them using SAF or the media store. Or with classic file means when requested all files access.
The user can see them using the default Files app on the device.
Where would the recommended location be to store an app licence file and a SQLite database for Android? Also, what are the constants used to point to those locations?
Note that the location(s) must be accessible without having to root the device, so the app data folder is not an option. I need to be able to access the files via a PC using a standard file manager.
I noticed that on the root folder of the device, there is a folder called "db" where other apps seem to store data. Is that a good location to store my db? If so, what is the Environment constant that points to it?
Depending on the level of security you want to achieve, there are numerous approaches to this issue.
At it's simplest you could store them at any folder on you external storage(Documents,Downloads e.t.c) or on your SD card provided that the user has given you permission AND has himself selected the path since newer Android Versions have reworked(restricted) the way an app can read/write from/to an SD card.
If for example you want to store it to "documents" folder you could do the following:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),".dir");
This selects the "Documents" folder and creates a folder called dir. Note the "." before "dir" , meaning that the folder will be invisible as a first level of security. From here on you can create any number of directories or files you desire.
So I have an app which is a music player.
Many times a user plays a song and wants to delete it. Many times he finds that there duplicates of files and hence wants to delete it.
Many users have more than thousands of songs and it would not be an appropriate solution if the user has to to pin point the location of the file he wants to delete.
I came across this How to use the new SD card access API presented for Android 5.0 (Lollipop)?
and it tells how to create the files given the whole uri.
But The appropriate solution would be if he could just choose the root directory(SD CARD ) instead of the whole path and grant the required permission and the app could manage the rest. This is exactly I want to achieve.
Given that i have only the file path of the file to be deleted and the uri of the root directory from the sd card access framework, what is the best way of deleting the file?
is that even possible ? or each time a user has to delete a file, he will have to pin point the location of that file as well?
The link you mentioned (How to use the new SD-Card access API presented for Lollipop) gives instructions on how to prompt user to choose the root directory using ACTION_OPEN_DOCUMENT_TREE.
After the user chooses the root node then your code will have a DocumentFile that represents the root node. You say you already have the file path. If so then use the segments of the file path and follow the path down the hierarchy using DocumentFile.html#listFiles().
You'll finally have the DocumentFile that represent the file you want to delete, then call DocumentFile.html#delete()
do you know how to create a folder in which photos will be saved and os and other apps should see that folder as photo album? I need to save photos in a certain album and if album does not exists, I need to create it.
Thanks
If you store images files anywhere on the external storage (with the exception of locations that also contain a .nomedia file), the media scanner, and thus the built-in "Gallery" app, will locate them and display them.
To be a good citizen you should place such files in the recommended location on the external storage using getExternalStoragePublicDirectory. You can pass that DIRECTORY_PICTURES for images.
Be advised though, that stuff is only available as of API level 8. If you need to also support earlier API versions you can use getExternalStorageDirectory and still use the correct recommended path on your own.
And, as always, you need to make sure the external storage is available before you try to write to it.
I have made an app that you puss a button and hear a ringtone!How can i add an option to save it to your device as a ringtone,if you like?thanks
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
It should be stored on a sdcard /Ringtones
If you want to save files that are not specific to your application and that should not be deleted when your application is uninstalled, save them to one of the public directories on the external storage. These directories lay at the root of the external storage, such as Music/, Pictures/, Ringtones/, and others.
and
In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.
I read somewhere that you can put them in /media/ringtones but this isn't documented anywhere else. So you should use what they wrote in the docs.