Open In App

How to Fetch Audio file From Storage in Android?

Last Updated : 11 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Selecting an audio file from the phone’s storage is required when the user is uploading or sending an audio file in your application. So this article primarily focuses on getting the audio files as a URI from your phone’s storage.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

The XML layout of our app contains one Textview and if we click on it we can pick an audio file from our storage then as we pick it the text view’s text changes to audio pick. Use the following code to implement the XML layout. 

XML




<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000022"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/select_Audio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Click to Select an Audio"
        android:textColor="#CAC6C6"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />
      
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Work with MainActivity.java file

Here we trigger intent of type “Audio” and action as  ACTION_GET_CONTENT. Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
  
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    private final int PICK_AUDIO = 1;
    Uri AudioUri;
    TextView select_Audio;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        select_Audio = findViewById(R.id.select_Audio);
  
        // SETTING ONCLICK LISTENER ------ ON TEXT VIEW CLICK TO TAKE AUDIO INPUT
        select_Audio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent audio = new Intent();
                audio.setType("audio/*");
                audio.setAction(Intent.ACTION_OPEN_DOCUMENT);
                startActivityForResult(Intent.createChooser(audio, "Select Audio"), PICK_AUDIO);
  
            }
        });
    }
  
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
          
        super.onActivityResult(requestCode, resultCode, data);
  
        if (requestCode == PICK_AUDIO && resultCode == RESULT_OK) {
            // Audio is Picked in format of URI
            AudioUri = data.getData();
            select_Audio.setText("Audio Selected");
        }
    }
}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads