0

May Be this works on before Lolipop/Kitkat but I didn't test it. I am testing this on marshmallow on my xperia m2 aqua and It's not displaying music from my removable sdcard, so, I copied two music from sdCard to Internal Storage and it's displaying those two music. Other music player are working well on phone. Below is my code:

public class ListviewActivity extends AppCompatActivity {
ListView listView;
String[] items;
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_songs);

    listView= (ListView) findViewById(R.id.listView);


     ArrayList<File> mySongs = findSong(Environment.getExternalStorageDirectory());
    Log.d("myLog"," "+Environment.getExternalStorageState());
    Log.d("myLog"," "+Environment.getExternalStorageDirectory());
    Log.d("myLog"," "+Environment.getExternalStorageDirectory().getAbsolutePath());



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

    for (int i = 0; i < mySongs.size(); i++) {
        Toast.makeText(this, "  "+mySongs.get(i).getName().toString(), Toast.LENGTH_SHORT).show();
        //  items[i] = mySongs.get(i).getName().toString();
        items[i] = mySongs.get(i).getName().toString();
    }
    arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
    Log.d("myLog"," "+items);



    listView.setAdapter(arrayAdapter);
}
public ArrayList<File> findSong(File root) {

    ArrayList<File> al = new ArrayList<File>();
    File[] files = root.listFiles();    // All file and folder automatic collect

    for (File singleFile : files) {
        if (singleFile.isDirectory() && !singleFile.isHidden()) {
            al.addAll(findSong(singleFile)); //Recursively call
        } else {
            if (singleFile.getName().endsWith(".mp3")) {
                al.add(singleFile);
            }
        }
    }
    return al;
}
}

Log.d from above code results

mounted
/storage/emulated/0
/storage/emulated/0

Manifest Permisson

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And Permisson Programmically for Marshmallow and higher

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

        }else{
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 738);

        }
    }

Result Its only displaying songs from internal storage. I have added two songs to internal storage for checking, and it displyed. Why not reading from sdCard although I have set the permission and all working code??

UPDATE

@Manoj has flag this question as possible duplicate but it's not true. I have give a proper permission and if user also hits on deny then it will ask again next time. In my code I have use if/else for permission, I think @Manoj didn't see it clearly..

apple appala
  • 474
  • 6
  • 14
  • Possible duplicate of [Android M - check runtime permission - how to determine if the user checked "Never ask again"?](http://stackoverflow.com/questions/30719047/android-m-check-runtime-permission-how-to-determine-if-the-user-checked-nev) – Manoj Perumarath Mar 20 '17 at 05:37
  • @ManojFrekzz Hey, You can see the code above I have posted Permission Programmically for Marshmallow and higher, I have used there if , else to check that. If the user deny also, it will ask next time. – apple appala Mar 20 '17 at 05:49
  • 2
    Of course it will not show files from SD card as you have no code for it. You only look at getExternalStorageDirectory() which is not the SD card but external memory which you called internal memory. But look at the function name. – greenapps Mar 20 '17 at 07:41
  • @greenapps Are you sure? Will you please answer me how could I get songs from sdcard ? – apple appala Mar 20 '17 at 08:20
  • Well you could answer that yourself too. You only need to know the full path to the sd card. You dont need other code. – greenapps Mar 20 '17 at 08:36
  • @greenapps I don't know, How can I goto sdCard. It would be better if you answer with some little explanation. What I learnt is getExternalDirectory() is primary/exteral storage written here. https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory() Please Please Please, Aswer me for getting more clearer on this!! ;) Thank you – apple appala Mar 20 '17 at 09:20

1 Answers1

0

The subject of your post is completely wrong as the only thing you want to know is How to detetermine the path to an SD card.

Well this is asked at least two times every week on this site so with a little reading you would have found it.

But have a look at the second item returned by getExternalFilesDirs().

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I am not getting you. getExternalFiesDirs(); retruns data from sdCard/Android/data/my-package-name/ right? as mentioned here http://stackoverflow.com/questions/10123812/diff-between-getexternalfilesdir-and-getexternalstoragedirectory but I need all .mp3 from /sdCard/ (root directory and subFolders) – apple appala Mar 20 '17 at 10:33
  • It returns TWO paths. Two File objects. About which path are you talking? And your link is about getExternalFilesDir() which is a quit different function which only returns one item. – greenapps Mar 20 '17 at 10:36
  • I can't use that because that are added only in api 19 while my minSdk is 14 what can i do? – apple appala Mar 20 '17 at 10:57
  • 1
    Try System.getenv(SECONDARY_STORAGE);. Be prepared that it can return null. If that does not work then add a directory picker and let the user choose the SD card. – greenapps Mar 20 '17 at 11:08
  • No, no, I am not able to do. I am so tired.From yesterday morning I was trying to fix this but not able to :( N I am loosing hope. Why it's so hard to access External storage? I am near to getExternalFilesDirs(), I pass null as string parameter and the program crash. – apple appala Mar 20 '17 at 11:31
  • It is not hard accessing external storage. Nor is it hard to access an SD card. The only thing that is problematic is finding the path of the SD card. – greenapps Mar 20 '17 at 11:36
  • Catch the exception that causes your app to crash. The logcat will tell you which one. Its probably NoSuchMethodError. – greenapps Mar 20 '17 at 11:40
  • what should I pass inside getExternalFilesDirs() ? null, "/","/sdcard/" or something else? – apple appala Mar 20 '17 at 11:46
  • getting http://stackoverflow.com/questions/29825198/nullpointerexception-attempt-to-get-length-of-null-array-readdirectory this similar error, may be Chris K. (First Answer) has said correct, it will return null if path does not exist. I tried "/" and "/sdcard/" – apple appala Mar 20 '17 at 11:50
  • i have tried with null, I did google and theres no good post I find. – apple appala Mar 20 '17 at 11:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138527/discussion-between-apple-appala-and-greenapps). – apple appala Mar 20 '17 at 11:52
  • okay, I m feeling so tired today. For example, Xender app is showing my location as /storage/emulated/0/ as Phone Storage and /storage/476C-1BE5/ as SD Card. This is strength.. – apple appala Mar 20 '17 at 12:01
  • but its not way to define sdcard location as /storage/476C-1BE65/ because this will defer according to device right? – apple appala Mar 20 '17 at 12:02