Picking up an audio file android

后端 未结 5 1050
花落未央
花落未央 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:28
    final Uri uri=Uri.parse(Environment.getExternalStorageDirectory()+"/Audio/abc.mp3");
    

    Replace /Audio/abc.mp3 with your path of mp3 file on sdcard.

    Dont forget to check if the external storage is mounted. Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2021-01-31 19:41

    No need to add type here like audio/*. It will crash to search such type of action. Try this. It worked for me.

    val intent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(intent, AUDIO_REQUEST_CODE)
    
    0 讨论(0)
  • 2021-01-31 19:45
     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);
        }
    
    0 讨论(0)
  • 2021-01-31 19:47

    //To pick audio file from device. Place below code in calling activity

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

    // Override onActivityForResult method in activity

    @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);
    }
    
    0 讨论(0)
提交回复
热议问题