where = null;\r
selectionArgs = null;\r
} else {\r
+ // Remove schema for search in the database\r
+ // Otherwise the file will not found\r
+ String data = path;\r
+ if( data.startsWith("file://") ){\r
+ data = data.substring(7);\r
+ }\r
uri = MediaStore.Audio.Media.getContentUriForPath(path);\r
where = MediaColumns.DATA + "=?";\r
selectionArgs = new String[] {\r
- path\r
+ data\r
};\r
}\r
\r
}\r
\r
/**\r
+ * Method that query the media database for search a path an translate\r
+ * to the internal media id\r
+ *\r
+ * @param path The path to search\r
+ * @return long The id of the resource, or -1 if not found\r
+ */\r
+ public long getIdFromPath(String path) {\r
+ try {\r
+ // Remove schema for search in the database\r
+ // Otherwise the file will not found\r
+ String data = path;\r
+ if( data.startsWith("file://") ){\r
+ data = data.substring(7);\r
+ }\r
+ ContentResolver resolver = getContentResolver();\r
+ String where = MediaColumns.DATA + "=?";\r
+ String selectionArgs[] = new String[] {\r
+ data\r
+ };\r
+ Cursor cursor =\r
+ resolver.query(\r
+ MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,\r
+ mCursorCols, where, selectionArgs, null);\r
+ try {\r
+ if (cursor == null || cursor.getCount() == 0) {\r
+ return -1;\r
+ }\r
+ cursor.moveToNext();\r
+ return cursor.getLong(IDCOLIDX);\r
+ } finally {\r
+ try {\r
+ if( cursor != null )\r
+ cursor.close();\r
+ } catch (Exception ex) {\r
+ }\r
+ }\r
+ } catch (UnsupportedOperationException ex) {\r
+ }\r
+ return -1;\r
+ }\r
+\r
+ /**\r
* Starts playback of a previously opened file.\r
*/\r
public void play() {\r
public String getArtistName() {\r
synchronized (this) {\r
if (mCursor == null) {\r
- return null;\r
+ return getString(R.string.unknown);\r
}\r
return mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.ARTIST));\r
}\r
public String getAlbumName() {\r
synchronized (this) {\r
if (mCursor == null) {\r
- return null;\r
+ return getString(R.string.unknown);\r
}\r
return mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.ALBUM));\r
}\r
public String getTrackName() {\r
synchronized (this) {\r
if (mCursor == null) {\r
- return null;\r
+ return getString(R.string.unknown);\r
}\r
return mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));\r
}\r
}\r
\r
@Override\r
+ public long getIdFromPath(String path) {\r
+ return mService.get().getIdFromPath(path);\r
+ }\r
+\r
+ @Override\r
public int getQueuePosition() {\r
return mService.get().getQueuePosition();\r
}\r