Picking up an audio file android

后端 未结 5 1057
花落未央
花落未央 2021-01-31 19:11

I need to fetch an audio file from SD Card and play it. I think this can be done by getting URI of an audio file. So, to pick an audio file I\'m using following code:

         


        
5条回答
  •  旧巷少年郎
    2021-01-31 19:35

    You can put below codes in your project when you want to select audio.

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

    And override onActivityResult in the same Activity, as below

    @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);
    }
    

提交回复
热议问题