22

我需要从 SD 卡中获取音频文件并播放。我认为这可以通过获取音频文件的 URI 来完成。因此,要选择一个音频文件,我使用以下代码:

Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Audio "), reqCode);

现在,我可以浏览音频文件并选择其中一个。

问题:如何在我的 onActivityResult 中读取选取文件的 URI?

4

5 回答 5

42

当您想选择音频时,您可以在项目中添加以下代码。

Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);

并在同一个Activity中覆盖onActivityResult,如下

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

  if(requestCode == 1){

    if(resultCode == RESULT_OK){

        //the selected audio.
        Uri uri = data.getData(); 
    }
  }
  super.onActivityResult(requestCode, resultCode, data);
}
于 2013-07-30T11:01:47.343 回答
6
 first of all open gallery through intent -
  public void openGalleryForAudio() {
        Intent videoIntent = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(videoIntent, "Select Audio"), AUDIO_REQUEST);
}


Then onActivityResult you should catch data - 
if (requestCode == AUDIO_REQUEST && null != data) {
                if (requestCode == AUDIO_REQUEST) {

                    Uri uri = data.getData();
                    try {
                        String uriString = uri.toString();
                        File myFile = new File(uriString);
                        //    String path = myFile.getAbsolutePath();
                        String displayName = null;
                        String path2 = getAudioPath(uri);
                        File f = new File(path2);
                        long fileSizeInBytes = f.length();
                        long fileSizeInKB = fileSizeInBytes / 1024;
                        long fileSizeInMB = fileSizeInKB / 1024;
                        if (fileSizeInMB > 8) {
                            customAlterDialog("Can't Upload ", "sorry file size is large");
                        } else {
                            profilePicUrl = path2;
                            isPicSelect = true;
                        }
                    } catch (Exception e) {
                        //handle exception
                        Toast.makeText(GroupDetailsActivity.this, "Unable to process,try again", Toast.LENGTH_SHORT).show();
                    }
                    //   String path1 = uri.getPath();

                }
            }

 This function is use for absolute path of audio file
 private String getAudioPath(Uri uri) {
        String[] data = {MediaStore.Audio.Media.DATA};
        CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
于 2018-01-19T11:34:25.557 回答
0
final Uri uri=Uri.parse(Environment.getExternalStorageDirectory()+"/Audio/abc.mp3");

替换/Audio/abc.mp3your path of mp3 file on sdcard

不要忘记检查外部存储是否已安装。 Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

于 2013-07-30T10:54:40.457 回答
0

//从设备中选择音频文件。将下面的代码放在调用活动中

int REQUEST_CODE = 1001;

 Intent audioIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
 startActivityForResult(audioIntent,REQUEST_CODE);

// 覆盖onActivityForResultactivity中的方法

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

  if(requestCode == REQUEST_CODE && resultCode == RESULT_OK ){

        //the selected audio.Do some thing with uri
        Uri uri = data.getData(); 


  }

  super.onActivityResult(requestCode, resultCode, data);
}
于 2019-03-06T11:02:15.193 回答
0

无需在此处添加类型,如音频/*。搜索此类操作会崩溃。尝试这个。它对我有用。

val intent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, AUDIO_REQUEST_CODE)
于 2020-08-24T07:16:52.957 回答