the code get music files from sdcard/music and view list of theme
in android 5 force close occur
I think the problem is this function
FileExtensionFilter()
package com.ghs.musicsatsametime;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/Music");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.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);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
and my logcat
10-30 10:45:30.176: E/AndroidRuntime(15788): FATAL EXCEPTION: main
10-30 10:45:30.176: E/AndroidRuntime(15788): Process: com.ghs.musicsatsametime, PID: 15788
10-30 10:45:30.176: E/AndroidRuntime(15788): java.lang.NullPointerException: Attempt to get length of null array
10-30 10:45:30.176: E/AndroidRuntime(15788): at com.ghs.musicsatsametime.SongsManager.getPlayList(SongsManager.java:25)
10-30 10:45:30.176: E/AndroidRuntime(15788): at com.ghs.musicsatsametime.dj.DJMusicFragment.getMusicList(DJMusicFragment.java:421)
10-30 10:45:30.176: E/AndroidRuntime(15788): at com.ghs.musicsatsametime.dj.DJMusicFragment.access$22(DJMusicFragment.java:415)
10-30 10:45:30.176: E/AndroidRuntime(15788): at com.ghs.musicsatsametime.dj.DJMusicFragment$7.onClick(DJMusicFragment.java:359)
10-30 10:45:30.176: E/AndroidRuntime(15788): at android.view.View.performClick(View.java:4856)
10-30 10:45:30.176: E/AndroidRuntime(15788): at android.view.View$PerformClick.run(View.java:19956)
10-30 10:45:30.176: E/AndroidRuntime(15788): at android.os.Handler.handleCallback(Handler.java:739)
10-30 10:45:30.176: E/AndroidRuntime(15788): at android.os.Handler.dispatchMessage(Handler.java:95)
10-30 10:45:30.176: E/AndroidRuntime(15788): at android.os.Looper.loop(Looper.java:211)
10-30 10:45:30.176: E/AndroidRuntime(15788): at android.app.ActivityThread.main(ActivityThread.java:5371)
10-30 10:45:30.176: E/AndroidRuntime(15788): at java.lang.reflect.Method.invoke(Native Method)
10-30 10:45:30.176: E/AndroidRuntime(15788): at java.lang.reflect.Method.invoke(Method.java:372)
10-30 10:45:30.176: E/AndroidRuntime(15788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
10-30 10:45:30.176: E/AndroidRuntime(15788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
CodePudding user response:
Have you added the following permission to your AndroidManifest.xml?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
External storage read access is required to list files in a directory.
CodePudding user response:
You variable home is null.
Gets a list of the files in the directory represented by this file. This list is then filtered through a FileFilter and matching files are returned as an array of files. Returns null if this file is not a directory. If filter is null then all files match.
See here: Throwing null pointer exception in Android
My first guess is you don't have any permissions.
CodePudding user response:
IMO, you should try that: put "Music" folder into your phone storage ("Device storage"). The log showed that your file path is wrong so listFiles() return null
final String MEDIA_PATH = Environment.getExternalStorageDirectory() "/Music/");
CodePudding user response:
Based on you log, which says:
java.lang.NullPointerException: Attempt to get length of null array
I found this line in your code:
if (home.listFiles(new FileExtensionFilter()).length > 0) {
The listFiles() methods returns a null array. It didn't event init or alloced. Thus, I guess a little protection on this line would help this crash:
List<File> files = home.listFiles(new FileExtensionFilter());
if (null != files && files.length > 0) {
for(File file:files){
...
}
}
this line will pretect code from crashing , but I don't know whether this line returns the correct array you need. Is this crash happened when there is no file under this path?
update: thx @JHH's suggestion
CodePudding user response:
Thanks all
the problem solved
I changed
home.listFiles(new FileExtensionFilter()).length > 0
to
home.listFiles(new FileExtensionFilter()).length != 0