SongsManagerを使用していてもロードMP3ファイルは、非同期タスク
public class SongsManager {
public ArrayList<HashMap<String, String>> getPlayList(String rootPath) {
ArrayList<HashMap<String, String>> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
HashMap<String, String> song = new HashMap<>();
song.put("file_path", file.getAbsolutePath());
song.put("file_name", file.getName());
fileList.add(song);
}
}
return fileList;
} catch (Exception e) {
return null;
}
}
}
PlaylistActivity私はのit.TheロードでのTextViewとrecyclerviewにすべての私のmp3ファイルをロードしています
public class PlaylistActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private SongsAdapter songsAdapter;
private SongsManager songsManager;
String MEDIA_PATH = Environment.getExternalStorageDirectory() + "";
ArrayList<HashMap<String, String>> songList=new ArrayList<>();;
MyTask myTask;
List<MyTask> myTasks;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);
myTasks=new ArrayList<>();
recyclerView=(RecyclerView)findViewById(R.id.playlistactivityrecyclerview);
progressBar=(ProgressBar)findViewById(R.id.progressbar);
myTask=new MyTask();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
} else {
myTask.execute();
}
}
}
public void updateRecycler(){
songsAdapter=new SongsAdapter(songList,getApplicationContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(songsAdapter);
}
public class MyTask extends AsyncTask<Void,String, ArrayList<HashMap<String, String>>>{
@Override
protected void onPreExecute() {
super.onPreExecute();
if(myTasks.size()==0){
progressBar.setVisibility(View.VISIBLE);
}
myTasks.add(this);
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
songsManager=new SongsManager();
songList =songsManager.getPlayList(MEDIA_PATH);
return songList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> hashMaps) {
super.onPostExecute(hashMaps);
myTasks.remove(this);
if(myTasks.size()==0){
progressBar.setVisibility(View.INVISIBLE);
}
if(hashMaps==null){
Toast.makeText(getApplicationContext(),"No mp3 Files",Toast.LENGTH_LONG).show();
return;
}
updateRecycler();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==0){
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
myTask.execute();
}
}
}
}
mp3ファイルには多くの時間がかかります。私がAsyncTaskを使用しても、時間がかかります。私が間違っていることを教えてください。私はasynctaskなしでそれをやってみたが、あまりにも時間がかかる。mp3ファイルを取得するための効率的な方法はありますか?
「私は間違っていますか? - すべての索引付きMP3コンテンツについて「MediaStore」を照会していません。 – CommonsWare