0

Im trying to get all the external files for a mp3 player. However, im getting an error saying Environment.getExternalStorageDirectory() is deprecated. I tried the solutions listed in a similar stackoverflow question but there not working for me for some reason; getExternalFilesDir(), android:requestLegacyExternalStorage="true" etc. If someone can provided a more detailed answer on how I can resolve this it would be greatly appreacited.

Also if the fix involves using internal storage not an external sd card that would work for me too.

This is my code:

package com.example.musicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ListView listview;
        super.onCreate(savedInstanceState);
        Context mContext = this;
        setContentView(R.layout.activity_main);
        listview =findViewById(R.id.listview);
        Dexter.withContext(this)
                .withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
                        ArrayList<File> mySongs = fetchSongs(Environment.getExternalStorageDirectory());
                        String[] items = new String[mySongs.size()];
                        for (int i =0; i<mySongs.size();i++){
                            items[i] = mySongs.get(i).getName().replace(".mp3","");
                            System.out.println(items);
                        }
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,items);
                        listview.setAdapter(adapter);
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {

                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
                        permissionToken.continuePermissionRequest();
                    }
                })
                .check();

    }
 public ArrayList<File> fetchSongs(File file){
        ArrayList arrayList = new ArrayList();
        File [] songs = file.listFiles();
        if (songs!=null){
            for(File myFile:songs){
                if(!myFile.isHidden() && myFile.isDirectory()){
                    arrayList.addAll(fetchSongs(myFile));
                }
                else{
                    if(myFile.getName().endsWith(".mp3") && !myFile.getName().startsWith(".")){
                        arrayList.add(myFile);
                    }
                }
            }
        }
        return arrayList;
 }

}
Game Top
  • 23
  • 5
  • It sounds like maybe you already tried it: but what's wrong with substituting equivalent APIs, per this post: https://stackoverflow.com/a/57116787/421195 – paulsm4 Jun 07 '21 at 00:13
  • Ya so I tried mContext.getExternalFilesDir(null) it gives no errors but when I run the app it doesnt show any files. – Game Top Jun 07 '21 at 00:38
  • Please 1) For troubleshooting purposes, add `String destPath = mContext.getExternalFilesDir(null).getAbsolutePath()` to your code, 2) Run it (e.g. on your AVD) and compare it with the path(s) you expect to see, 3) Update your post, telling us a) the value of destPath, and b) what you expected. ALSO: Please re-read the replies, and comments, in the [SO link](https://stackoverflow.com/a/57116787/421195). *ALL* of them. They could help. Honest! – paulsm4 Jun 07 '21 at 01:55
  • Hey, so the path that is being returned is /storage/emulated/0/Android/data/com.example.musicplayer/files. This seems wrong cuz isn't this just the path to the app not the overall external storage. Also the app just crashes "Google Play Services keeps stopping" when I pass this path in to fetch song. – Game Top Jun 07 '21 at 02:26
  • Android has made a LOT of (mostly "security related") changes to storage, storage permissions and the various APIs for accessing the filesystem. Perhaps you're looking at example code that might have worked back in API 23 days? Please (re)read this: https://developer.android.com/guide/topics/providers/content-provider-basics, this: https://developer.android.com/training/data-storage/shared/documents-files and the entire thread here: https://stackoverflow.com/a/57116787/421195. – paulsm4 Jun 07 '21 at 03:05
  • `im getting an error saying Environment.getExternalStorageDirectory() is deprecated.` That is no error but a warning. A message. You just can use it. So your code should work. – blackapps Jun 07 '21 at 07:05
  • It complies but my listview is empty no mp3 files are showing up when I build it. – Game Top Jun 08 '21 at 03:56

0 Answers0