简体   繁体   English

SD卡无法在Android中访问

[英]SD card not getting accessed in android

I am facing continuous problem with accessing SD Card even after using Environment.getExternalStorageDirectory() or even directly using /sdcard/ application is fetching data from phone's internal memory .I am using moto e for testing 即使在使用Environment.getExternalStorageDirectory()或什至直接使用/sdcard/应用程序从手机内部存储器中获取数据后,我仍面临访问SD卡的持续问题。我正在使用moto e进行测试

Please help 请帮忙

public class SongsManager {

final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}




public ArrayList<HashMap<String, String>> getPlayList(){
    File home = new File(MEDIA_PATH);
    walkdir(home);





    return songsList;



}

public void walkdir(File dir) {
    String Pattern = ".mp3";
    File listFile[] = dir.listFiles();
    if (listFile != null) {
    for (int i = 0; i < listFile.length; i++) {
    if (listFile[i].isDirectory()) {
    walkdir(listFile[i]);
    } else {
    if (listFile[i].getName().endsWith(Pattern)){
        for (File file : dir.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
            song.put("songPath", file.getPath());

            // Adding each song to SongList
            songsList.add(song);
        }

    }
    }
    }  
    }  
    }

}


class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}

Instead of doing this: 而不是这样做:

final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
...
File home = new File(MEDIA_PATH);

You could just do this: 您可以这样做:

File home = Environment.getExternalStorageDirectory();

and then to call you method walkdir 然后调用你的方法walkdir

walkdir(home);

I think that the problem is in the getPath() method. 我认为问题出在getPath()方法中。 Maybe it should be the getAbsolutePath() method 也许应该是getAbsolutePath()方法

That is normal for Android. 这对于Android是正常的。 It's not easy to get the path to removable media on android. 在Android上获取可移动媒体的路径并不容易。 You can read a lot about this on this site. 您可以在此站点上阅读很多有关此的内容。 Add a directory picker to your app so the user canindicate it. 将目录选择器添加到您的应用程序,以便用户可以指示它。 Try getExternalFileDirs(). 尝试getExternalFileDirs()。 If it gives you two directories the latter will be the micro SD card. 如果它给您两个目录,则后者将是micro SD卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM