Android device Directory

Hi guys,

does anyone know how to set the directory of files that are stored in an android device ?

I am just testing an application that plays music but I don't know how to set the directory.

For ex.: in windows I know that I can set it like this ("C:\\User\\Music\\..."), what about in android OS ?

I have tried to debugg the aplication using the directory from windows in real device and virtual device emulator but the files are not displayed. I mean the list of files is empty. When I try this on visual studio the files are displayed, so the problem is with the directory of android OS.

I'd appreciate the help.

Thanks.

You can find the common 'home' folder using

http://man7.org/linux/man-pages/man3/getenv.3.html

Something like:  

String homeDir = String(getenv("EXTERNAL_STORAGE"));

 

 Btw, if you post Android specific questions in the Android forum it makes it easier for other users to find answers to Android specific questions laugh   

Thanks man, I'll try it. wink

My bad. cool

That's linux, not Android. I've no idea if the code above would actually work, but I wouldn't trust it - on Android you should use File::getSpecialLocation to find these folders, but be aware that your app may not be allowed access to them because of security permissions!

I tried File::getSpecialLocation and it doesn't work.

Maybe because getSpecialLocation works only on Windows, Mac or Linux.

Is there another method that I can use ?

No, it works on Android too, but it's a recent change - make sure you pull the latest version from github.

I asked several months ago about File::getSpecialLocation on Android but got no replies. It wasn't working in my tests. In the end I turned to this method which worked fine for me on numerous devices. But I'm glad I can now use File::getSpecialLocation, that'll make things easier. 

I pulled the latest version from github and when I ran my project I got an error in my device " Application has stopped ".

When I installed the version that I had the application ran perfect. But again in the older version I'm not able to get the directory. 

How can I fix this problem now ?

Do you get this error also when you don't try to access the location? i.e. exact the same code like in the previous version...

Does your App ask for permission to access the external storage in the manifest?

https://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

I used android in QT, not Juce, but these are things I would look at. Good Luck!

I just downloaded the last version from github and I fixed it now.

The application ran ok but still I'm not getting the files from android directory.

I'm explaining again what I want to get from the directory of my android phone.

I want to fill a listbox with the name of a mp3 file. ( I did this in windows and it works fine ).

The mp3 file is stored in my phone at user Music Directory.

Now, these are the codes i wrote for this to work :


int getNumRows() {

 File folder(File::getSpecialLocation(File::userMusicDirectory)); 
 return folder.getNumberOfChildFiles(File::findFiles, "*.mp3");

 }

 void paintListBoxItem(int rowNumber, Graphics& a, int width, int height, bool rowIsSelected) {
 if (!rowIsSelected) {
 a.setColour(Colour(0, 127, 255).brighter(0.3f));
 } else if (rowIsSelected) { 
a.fillAll(Colour(0, 127, 255).brighter(0.08f)); a.setColour(Colours::white);
 }
 a.setFont(height * 0.9f);

 File folder(File::getSpecialLocation(File::userMusicDirectory)); 
folder.findChildFiles(files, File::findFiles, true, "*.mp3"); 
a.drawText(" " + String(files.getReference(rowNumber).getFileName()), 5, 0, width, height, Justification::centredLeft, true); 
}

The only problem is with the location of the directory.

When I run the project the application opens but the list is empty.

In the list should appear the mp3 file name.

Maybe I should set the directory like this : File folder("/storage/emulated/0/Music/");

I'd appreciate the help.

Thanks. ;)

 

The best is to display the result of

File folder(File::getSpecialLocation(File::userMusicDirectory));
folder.getFullPathName();

and see, if it points to where you expect it. You can do this in any paint routine.
And that's the perfect moment to move the folder.findChildFiles() call out of the paint routine, be aware that the paint routine might be called very often (e.g. when scrolling the ListBox) and reading the folder's content will take some time.

You can set the folder pointing by hand, but then you are responsible yourself to adapt that for each plattform, each android version, if it changes. You are probably better of to use the abstraction layer that juce provides here.

Appearently android has two different types of locations, getExternalStoragePublicDirectory() and getExternalFilesDir(), which is app-private, see https://developer.android.com/guide/topics/data/data-storage.html#filesExternal maybe that helps debugging. Which is given by juce's getSpecialLocation() you will have to try, that's why you should display it's content somewhere...

Good Luck...

Thanks, I'll try it. ;)

I did get the name of the folder but not the mp3 files. :/

I just don't understand why the codes work on windows and not in android ?!

I thought it should have worked on both platforms.

 

So the folder points to the location you expected? And did you check with a file browser on your phone, that in this folder there are really mp3 files?

If that's the case, it should work. Albeit android has some mechanisms that shall prevent malicous apps from accessing private data of other apps, see: https://developer.android.com/training/articles/security-tips.html

Maybe somebody who is using juce on android should jump in here, if there are permissions to be set (like manifest etc.) and so on...

Good luck