1

我在 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;
}

此方法在 sdcard 上运行良好,

但在 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("", "");
    }

当上面的代码在循环中时它返回false

while (cursor.moveToNext())

但是当我在其他地方硬编码时,比如 onCreate

它返回真实...

那是什么情况?

4

0 回答 0