解析Android中所选音频文件的ID3标签

时间:2014-11-05 01:05:39

标签: android audio android-intent id3

我正在尝试解析所选音频文件的ID3标签信息

我正在启动一个Intent让用户选择一个文件

        public void onClick(View v) {
            Intent intent_upload = new Intent();
            intent_upload.setType("audio/*");
            intent_upload.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent_upload, 1);
        }
在onActivityResult中

我尝试将内容Uri解析为真实的文件路径。

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {

            if (resultCode == getActivity().RESULT_OK) {

                Uri uri = data.getData();
                Log.i("Yolo", data.getData().getPath());

                String realPath = getRealPathFromURI(getActivity(), uri);
                Log.i("Yolo", realPath);
                MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                mmr.setDataSource(realPath);

                mEditArtist
                        .setText(mmr
                                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
                mEditTitle
                        .setText(mmr
                                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

以下是解析文件路径的方法

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Audio.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null,
                null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

我无法解决问题,因为我收到了IllegalArgumentException

当我在不同位置选择不同的文件时,我会收到几个错误。这是其中之一(在SD卡上选择带有root brwoser的文件)

11-05 02:09:45.323: E/AndroidRuntime(14575): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=Intent { act=android.intent.action.GET_CONTENT dat=file:///storage/sdcard1/Sounds/Camo & Krooked/Cross The Line (Special Edition)-2011/[01] [Camo & Krooked] - Get Dirty (Feat. TC).mp3 cmp=com.jrummy.root.browserfree/com.jrummy.file.manager.FilePicker }} to activity {com.example.sharefrogg/com.example.sharefrogg.activity.MainActivity}: java.lang.NullPointerException

0 个答案:

没有答案