Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My App does not read .mp3 files from SD Card #2

Closed
ppda1996 opened this issue Feb 17, 2019 · 1 comment
Closed

My App does not read .mp3 files from SD Card #2

ppda1996 opened this issue Feb 17, 2019 · 1 comment

Comments

@ppda1996
Copy link

Here are my MainActivity.java codes but it does not read the .mp3 files from the SD card.

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.musicPlayer.patel.lastkingmusicplayer.R;

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

public class MainActivity extends AppCompatActivity {

ListView myListViewForSongs;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myListViewForSongs = (ListView) findViewById(R.id.mySongListView);


    MultiplePermissionsListener();

}






public void MultiplePermissionsListener() {

    Dexter.withActivity(this)
            .withPermissions(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                   )
            .withListener(new MultiplePermissionsListener() {
                @Override
                public void onPermissionsChecked(MultiplePermissionsReport report) {
                    // check if all permissions are granted
                    if (report.areAllPermissionsGranted()) {
                        // do you work now
                        display();
                    }

                    // check for permanent denial of any permission
                    if (report.isAnyPermissionPermanentlyDenied()) {
                        // permission is denied permanently, navigate user to app settings
                    }
                }

                @Override
                public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                    token.continuePermissionRequest();
                }
            }).check();
}



public ArrayList<File> findSong(File file) {

    ArrayList<File> arrayList = new ArrayList<>();

    File [] files = file.listFiles();

    for (File singleFile: files) {

        if(singleFile.isDirectory() && !singleFile.isHidden()){

            arrayList.addAll(findSong(singleFile));
        }
        else {
            if (singleFile.getName().endsWith(".mp3") ||
                    singleFile.getName().endsWith(".wav")) {

                arrayList.add(singleFile);
            }
        }
    }

    return arrayList;
}

void display(){

    final ArrayList<File> mySongs = findSong(Environment.getExternalStorageDirectory());

    items = new String[mySongs.size()];

    for (int i=0;i<mySongs.size();i++) {

        items[i] = mySongs.get(i).getName().toString().replace(".mp3","").replace(".wav","");

    }

    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
    myListViewForSongs.setAdapter(myAdapter);

    myListViewForSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {

            String songName = myListViewForSongs.getItemAtPosition(i).toString();

            startActivity(new Intent(getApplicationContext(),PlayerActivity.class)
                    .putExtra("songs",mySongs).putExtra("songname",songName)
                    .putExtra("pos",i));


        }
    });




}
@ansus25
Copy link

ansus25 commented Feb 15, 2021

Have you ever solved this issue? I have the same issue, but I can't find it anywhere. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants