1

I'm trying to include a folder browser element in my app, which upon clicking an audio file, locates that file in the MediaStore and returns it's _ID.

One of the issues I'm having is that for some devices, it seems the absolute file path works fine, but for other devices, I need the canonical paths. In some cases, it doesn't work at all!

String canonicalPath = file.getCanonicalPath();
String absolutePath = file.getAbsolutePath();

For what it's worth, the 'file' is created from the root "/", not from getExternalStorageDirectory() (because some Audio files may be located on the internal sd or elsewhere).

So the query looks like this:

Cursor cursor = getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols,  MediaStore.MediaColumns.DATA + "= ? ", new String[]{absolutePath}, null);

If it fails, I do the same query on the canonicalPath, and see if that works. The issue is that it fails in both cases on a lot of different devices, depending on the file structure.

On my particular device (Galaxy S2), the difference between the absolute and canonical paths is usually as follows:

/extSdCard/Music/myFile.mp3
/storage/sdcard1/Music/myFile.mp3

Am I wrong to assume the file will always have an ID in the MediaStore? Is there a better way to find the 'actual' path to the file, or a better way to construct the query to get the id of the file? It's crashing all over the place on various devices, due to the query not returning and cursor items (in other words, it can't find anywhere where the MediaColumns.DATA is equal to the absolute or canoncial path passed in.

I guess I'm just looking for a way to have a more failproof query.. A better way to find the path or the id of the audio file. Any ideas would be appreciated.

Tim Malseed
  • 6,003
  • 6
  • 48
  • 66

0 Answers0