无法从内部存储器检索mp3文件

时间:2018-09-17 14:00:29

标签: java android android-recyclerview

很抱歉,但是我是新手,尝试从手机的内部存储中检索所有mp3文件时遇到了一些问题。不知道为什么,但是我无法检索任何文件,即ArrayList获取空值

这是我来自PlaylistActivity的代码,这是我检索mp3文件的地方:

private static final int RC_READ = 1;
private RecyclerView mRecycler;
private ArrayList<File> mSongs;
private String[] mSongNames;
private MyAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playlist);

    mRecycler=(RecyclerView) findViewById(R.id.recyclerView);
    mRecycler.setLayoutManager(new LinearLayoutManager(this));
    mRecycler.setHasFixedSize(true);

    mSongs=new ArrayList<>();

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, RC_READ );
    }

    else {
        getSongsPlayList(); }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if(requestCode== RC_READ && grantResults[0]== PackageManager.PERMISSION_GRANTED){
        getSongsPlayList();
    }
}

private void getSongsPlayList(){
    Log.d("Music","getSongsPlayList()");
    mSongs = findSongs(Environment.getExternalStorageDirectory().getAbsolutePath());
    if (mSongs != null) {
        mSongNames = new String[mSongs.size()];
        for (int i = 0; i < mSongs.size(); i++) {
            mSongNames[i] = mSongs.get(i).getName().replace(".mp3", "").replace(".wav", "");
            Log.d("Music","Song name:"+mSongNames[i]);
        }
    } else {
        Log.d("Music", "Null");
        mSongNames = new String[1];
        mSongNames[0] = "Star Boy";
    }
    mAdapter = new MyAdapter(mSongNames, getApplicationContext());
    mRecycler.setAdapter(mAdapter);
}

private ArrayList<File> findSongs(String absolutePath) {
    ArrayList<File> mList=new ArrayList<>();

    try{
        File rootFolder=new File(absolutePath);
        File[] files=rootFolder.listFiles();

        for(File file: files){
            if(file.isDirectory()) {
                if (findSongs(file.getAbsolutePath()) != null) {
                    mList.addAll((findSongs(file.getAbsolutePath())));
                } else {
                    break;
                }
            } else if(file.getName().endsWith(".mp3") || file.getName().endsWith(".wav")){
                mList.add(file);
            }
        }
        return mList;
    }catch (Exception e){
        return null;
    }
}

这是我的recyclerview的MyAdapter类代码:

private String[] mSongs;
private Context mContext;

public MyAdapter(String[] mSongs, Context mContext) {
    this.mSongs = mSongs;
    this.mContext=mContext;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(mContext).inflate(R.layout.custom_row,parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.mSong.setText(mSongs[position]);

}

@Override
public int getItemCount() {
    return mSongs.length;

}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView mSong;
    public ViewHolder(View itemView) {
        super(itemView);
        mSong=(TextView) itemView.findViewById(R.id.songName);
    }
}

recyclerView仅显示一首歌曲,即Starboy,我正在使用它检查mSongs是否为null,并且显然在执行findSongs方法之后为null。我不知道怎么了。预先感谢!

PS:我为manifest.xml添加了必要的权限,如下所示:

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

1 个答案:

答案 0 :(得分:1)

我不确定100%,但是对我来说,您似乎并没有在寻求外部存储权限。

从Android 6.0(API级别23)开始,仅在AndroidManifest.xml中写入权限已不够。现在,您还必须在运行时期间请求权限。例如,在您的活动的<font size="8">Test env</font>方法中

请检查以下链接:
Storage permission error in Marshmallow
https://developer.android.com/training/permissions/requesting

如果这样做没有帮助,则需要检查LogCat中的异常或其他信息