我在sdcard和extsdcard都有一些音乐
我使用Cursor列出它们:
Uri deviceMusic = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String sortOrder = MediaStore.Audio.Media.DISPLAY_NAME;
Cursor cursor = this.getContentResolver().query(deviceMusic, null,
selection, null, sortOrder);
if (cursor == null || cursor.isClosed()) {
return;
}
// Get music information
if (cursor.moveToFirst()) {
// File path
String data = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
} while (cursor.moveToNext());
然后我使用下面的代码来判断音乐的类型:
public static String getFileHeader(String filePath) {
FileInputStream is = null;
String value = null;
try {
is = new FileInputStream(filePath);
byte[] b = new byte[3];
is.read(b, 0, b.length);
value = bytesToHexString(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
}
}
}
return value;
}
此方法适用于SD卡,
但在extsdcard上遇到“FileNotFoundException”
所以我尝试用硬编码检查文件:
File file = new File(
"/storage/extSdCard/(I Can't Help) Falling In Love With You - UB 40.mp3");
if (file.exists()) {
Log.i("", "");
}
在
循环中的上述代码时返回falsewhile (cursor.moveToNext())
但是当我在其他地方硬编码时,例如onCreate
它返回true ...
那是什么情况?