0

SD カード上のサウンド ファイルのフル パスを取得しようとしています。

これにより、サウンド ピッカーが起動します。次に、Play ミュージック アプリを使用してファイルを選択します。

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_PICK);
    intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RESULT_SOUNDPICKER);

アクティビティの結果で、フルパスを取得しようとしています

case RESULT_SOUNDPICKER: {
    Log.d("TAG", "onActivityResult "+requestCode+" "+resultCode);
        if (resultCode == RESULT_OK) 
        {
            Uri uri = data.getData();
            String filePath = uri.getPath();
            Log.d("TAG", "FilePath: "+filePath);

            // A song was picked.
            Log.d("TAG", "PickSongActivity.onActivityResult: "+data.getDataString());
        }
}

しかし、これは次のようなパスを返します

//media/external/audio/media/13085

ファイルが保持されている場所の適切なパスではなく。ファイルを再生するために使用したいので、完全なパスを取得する必要があります。

ありがとうございました。

解決策 このメソッドを使用して、フル パスを取得できます。

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
4

1 に答える 1

0

ファイルを再生するために使用したいので、完全なパスを取得する必要があります。

到達できるパスは言うまでもなく (ファイルである必要がないため) パスがない場合もあります (ファイルはアクセス可能なストレージ上にある必要がないため) 。

MediaPlayerUriを直接使用できるので、その方法をお勧めします。

于 2014-02-09T19:03:11.737 に答える