How to get all media files in /res/raw dynamically? - android

I want to initialize MediaPlayer instances for all of the soundfiles found in res/raw:
/res/raw/test1.mp3
/res/raw/test2.mp3
/res/raw/testN.mp3
Purpose is to play different samples on a button click, without delays.
List<MediaPlayer> player = new ArrayList<>();
//TODO how to loop properly?
for (Rawfile file : rawfiles) {
pl = MediaPlayer.create(getBaseContext(), R.raw.test1);
player.add(pl);
}
Lateron, if eg button2 is clicked:
player.get(1).start();
Question: how can I get the R.raw.* files dynamically during initialization of the app?
Update: the following is quite close, but there are 2 problems:
1) If eg only one file "test.mp3" is placed in my /res/raw folder, the function shows 3 files.
2) How can I then load those files to mediaplayer?
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Result:
I/Raw Asset:: $change
I/Raw Asset:: serialVersionUID
I/Raw Asset:: test

For the moment solved as follows, but feels kinda hacky:
public static List<Integer> listRawMediaFiles() {
List<Integer> ids = new ArrayList<>();
for (Field field : R.raw.class.getFields()) {
try {
ids.add(field.getInt(field));
} catch (Exception e) {
//compiled app contains files like '$change' or 'serialVersionUID'
//which are no real media files
}
}
return ids;
}

I Really forget from where i get this, it can be duplicated any way not my code but works perfectly :
private boolean listFiles(String path) {
String [] list;
try {
list = getAssets().list(path);
if (list.length > 0) {
// folder founded
for (String file : list) {
if (!listAssetFiles(path + "/" + file))
return false;
}
} else {
//file founded
}
} catch (IOException e) {
return false;
}
return true;
}

Though undervoted, this answer from CommonsWare is comprehensive. The best you can do is iterate over the raw fields by reflection. If you find non-resource fields, you should discard them manually (I see you did it in an answer).
One point: putting files in raw directory is done during development time, and programming the iteration over raw resources is also done during development time. It's a problem you should solve before compilation, rather than finding out what files you have in run time, that is, you should list the files by name in your code.

Related

NullPointerException: Attempt to get length of null array

I am developing an Android App. I have the below code to write a list of all files starting with XLR in a particular folder:
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
try {
if ((file.exists()) && (file != null)) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if (file.getName().startsWith("XLR")) {
inFiles.add(file);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return inFiles;
}
On Android 10 and 11 it seems to be a problem. For Android 10 I have legacy storage enabled and Android 11 I have got access to all files. So the issue is not file access.
The folder itself might not be in my app's folder, hence the need to be able to list out files
I know that there is an extensive page here on NPE's and I have tried my best to follow the advice there (I am checking that files exist and are not null), but still to no avail.
I feel that there's probably something so stupid, that a more experienced programmer would probably pick out in 2 seconds...
This problem would be easily solved just reading the documentation of the actual File package.
Documentation on the method File::listFiles:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
As you can see, that method can actually return null and not only an empty array, so you should check that the value isn't null before even trying to iterate over it.

MP3 JID3 Library amendments

Help needed
I use the JID3 library to edit mp3 tags. This works fine as long as the music is on an internal sdcard. However, for api 19+ this no longer works for the external sdcard.
I understand I need to implement the Storage Access Framework but have no idea how to go about this.
The way JID3 works is it reads the mp3 file, extracts the tag(s) under consideration, say artist, updates the tag(s) with the new value.
the following steps need amendment:
create a .tmp file in the actual folder where the track resides, which ends up as the actual track with amended tag(s)
delete original track
rename the tmp file with the original track name.
I posted the relevant pieces of code
public class MP3File extends MediaFile
// create temporary file to work with
try
{
oTmpFileSource = m_oFileSource.createTempFile("id3.", ".tmp");
}
catch (Exception e)
{
throw new ID3Exception("Unable to create temporary file.", e);
}
m_oFileSource.delete();
oTmpFileSource.renameTo(m_oFileSource);
public class FileSource implements IFileSource
public IFileSource createTempFile(String sPrefix, String sSuffix)
throws IOException
{
File oTmpFile = File.createTempFile("id3.", ".tmp", m_oFile.getAbsoluteFile().getParentFile());
return new FileSource(oTmpFile);
}
public boolean delete()
{
return m_oFile.delete();
}
public boolean renameTo(IFileSource oFileSource)
throws IOException
{
if ( ! (oFileSource instanceof FileSource))
{
throw new IOException("Cannot rename between different file source types.");
}
return m_oFile.renameTo(((FileSource)oFileSource).m_oFile);
}

how to efficiently get number of subdirectories and files

In my little file explorer app I show the number of sub-elements in each directory like:
To set those numbers, I launch an AsyncTask (so the ListView won't get "laggy") from my Adapter's getView() method and that for each item in the list. However, when viewing some system directories (like "/" for example) with huge number of subdirs and files, the garbage collector is going insane and performance drops significantly (multiple instances of my AsyncTask still stay in memory even after the app gets finished).
I'm quite sure this is related to how I implemented the subdirs and subfiles check that I'm doing inside the AsyncTask, but the following recursive approach is the only thing I could think of:
//countHidden is a boolean indicating whether to count hidden files
private int[] getSubFilesCount(File root) {
int fcount = 0;
int dcount = 0;
File[] files = root.listFiles();
if (files != null)
for (File f : files) {
if (f.isDirectory()) {
getSubFilesCount(f);
if (f.isHidden()) {
if (countHidden)
dcount++;
} else {
dcount++;
}
} else {
if (f.isHidden()) {
if (countHidden)
fcount++;
} else {
fcount++;
}
}
}
int[] tcount = { fcount, dcount };
return tcount;
}
The question: is there any alternative to get the number of subdirectories and files that will work faster then the approach posted above?
You could do what's suggested here which is effectively the same as you're doing but slightly more succinct (if you don't mind me saying so). Obviously you'd have to do it for files and directories. I believe Java 7 provides a better way of doing this but unfortunately you're limited to Java 6 at the moment with Android.

Create a fast file search function in android

I'm new to android and I'm trying to develop file explorer which includes search function. I'm using a recursive search function that works fine in folders with a few subfolders and files, but for some reason it's EXTREMELY SLOW and could "Force Close" in folders with lots of subfolders and files, because there's not enough memory. I do the search by creating ArrayList where the results will be placed, and then calling the recursive function that will fill the list. The "path" argument is the file where the search will start from, and "query" is the search query.
ArrayList<File> result = new ArrayList<File>();
fileSearch(path, query, result);
this is what the recursive function looks like:
private void fileSearch(File dir, String query, ArrayList<File> res) {
if (dir.getName().toLowerCase().contains(query.toLowerCase()))
res.add(dir);
if (dir.isDirectory() && !dir.isHidden()) {
if (dir.list() != null) {
for (File item : dir.listFiles()) {
fileSearch(item, query, res);
}
}
}
}
If someone could point me to a way of performing a faster and/or more efficient file search, I would really appreciate that.
EDIT:
This is how I tried to do the job with AsyncTask:
private class Search extends AsyncTask<File, Integer, Void> {
String query;
ArrayList<File> result = new ArrayList<File>();
public Search(String query){
this.query = query;
setTitle("Searching");
}
#Override
protected Void doInBackground(File... item) {
int count = item.length;
for (int i = 0; i < count; i++) {
fileSearch(item[i], query, result);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
setProgress(progress[0]);
}
protected void onPostExecute() {
searchResults = new ListItemDetails[result.size()];
for (int i = 0; i < result.size(); i++) {
File temp = result.get(i);
if (temp.isDirectory())
searchResults[i] = new ListItemDetails(temp.getAbsolutePath(),
R.drawable.folder, temp.lastModified(), temp.length());
else {
String ext;
if (temp.getName().lastIndexOf('.') == -1)
ext = "";
else
ext = temp.getName().substring(
temp.getName().lastIndexOf('.'));
searchResults[i] = new ListItemDetails(temp.getAbsolutePath(),
getIcon(ext), temp.lastModified(), temp.length());
}
}
finishSearch();
}
}
public void finishSearch() {
Intent intent = new Intent(this, SearchResults.class);
startActivity(intent);
}
The call to finishSearch() is just so I can create the Intent to show the results in other Activity. Any ideas, suggestions, tips? Thanks in advance
It is possible that you are hitting symbolic links and going into an infinitive loop with your search function and depleting available memory to your application.
I would suggest you to keep a separate list containing canonical paths (File.getCanonicalPath()) of directories you've visited and avoid visiting them over and over again.
Why don't you use Apache Commons IO? It has some functions to deal with searching.
I also suggest using the method FileUtils.listFiles, which takes a folder, a search query and a directory filter as parameters.
The following example returns you a list of all file's paths that matched according to a regex. Try adding it in doInBackground of your AsyncTask:
Collection files = FileUtils.listFiles(new File(yourRootPath),
new RegexFileFilter(searchQuery),
DirectoryFileFilter.DIRECTORY);
Have you looked into Lucene?
It is especially designed to index and query large numbers of free-text documents, so many of the I/O streaming and indexing tasks have already been solved for you. If you remove the recursion and do the document indexing using a Lucene index in a purely iterative fashion, memory issues may be mitigated.
Look into this thread:
Lucene in Android
Do it in the background, and starting from Android O (API 26) , you can use Files.find API. Exmaple:
Files.find(
Paths.get(startPath), Integer.MAX_VALUE,
{ path, _ -> path.fileName.toString() == file.name }
).forEach { foundPath ->
Log.d("AppLog", "found file on:${foundPath.toFile().absolutePath}")
}

How can I clear the Android app cache?

I am writing a app which can programatically clear application cache of all the third party apps installed on the device. Following is the code snippet for Android 2.2
public static void trimCache(Context myAppctx) {
Context context = myAppctx.createPackageContext("com.thirdparty.game",
Context.CONTEXT_INCLUDE_CO|Context.CONTEXT_IGNORE_SECURITY);
File cachDir = context.getCacheDir();
Log.v("Trim", "dir " + cachDir.getPath());
if (cachDir!= null && cachDir.isDirectory()) {
Log.v("Trim", "can read " + cachDir.canRead());
String[] fileNames = cachDir.list();
//Iterate for the fileName and delete
}
}
My manifest has following permissions:
android.permission.CLEAR_APP_CACHE
android.permission.DELETE_CACHE_FILES
Now the problem is that the name of the cache directory is printed but the list of files cachDir.list() always returns null. I am not able to delete the cache directory since the file list is always null.
Is there any other way to clear the application cache?
"android.permission.CLEAR_APP_CACHE" android.permission.DELETE_CACHE_FILES"
Ordinary SDK applications cannot hold the DELETE_CACHE_FILES permission. While you can hold CLEAR_APP_CACHE, there is nothing in the Android SDK that allows you to clear an app's cache.
Is there any other way to clear the application cache?
You are welcome to clear your own cache by deleting the files in that cache.
Check out android.content.pm.PackageManager.clearApplicationUserData: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/content/pm/PackageManager.java/
The other hidden methods in that class might be useful, too.
In case you've never used hidden methods before, you can access hidden methods using Java reflection.
poate iti merge asta
static int clearCacheFolder(final File dir, final int numDays) {
int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
try {
for (File child:dir.listFiles()) {
//first delete subdirectories recursively
if (child.isDirectory()) {
deletedFiles += clearCacheFolder(child, numDays);
}
//then delete the files and subdirectories in this dir
//only empty directories can be deleted, so subdirs have been done first
if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
if (child.delete()) {
deletedFiles++;
}
}
}
}
catch(Exception e) {
Log.e("ATTENTION!", String.format("Failed to clean the cache, error %s", e.getMessage()));
}
}
return deletedFiles;
}
public static void clearCache(final Context context, final int numDays) {
Log.i("ADVL", String.format("Starting cache prune, deleting files older than %d days", numDays));
int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
Log.i("ADVL", String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
}
I'm not sure how appropriate this is in terms of convention, but this works so far for me in my Global Application class:
File[] files = cacheDir.listFiles();
for (File file : files){
file.delete();
}
Of course, this doesn't address nested directories, which might be done with a recursive function like this (not tested extensively with subdirectories):
deleteFiles(cacheDir);
private void deleteFiles(File dir){
if (dir != null){
if (dir.listFiles() != null && dir.listFiles().length > 0){
// RECURSIVELY DELETE FILES IN DIRECTORY
for (File file : dir.listFiles()){
deleteFiles(file);
}
} else {
// JUST DELETE FILE
dir.delete();
}
}
}
I didn't use File.isDirectory because it was unreliable in my testing.

Categories

Resources