Reputation: 4535
I am new to android development. I would like create a simple app that loads all the music files from music into a list view. to do that I created two files like follows,
package com.chandra.TestMedia;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ListAdapter;
public class TestMediaActivity extends ListActivity {
/** Called when the activity is first created. */
private static final String MEDIA_PATH = new String("/sdcard/");
List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private int currentPosition = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateSongList();
}
public void updateSongList()
{
System.out.println("home.list().length ");
File home = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Music");
System.out.println("home.list().length " + home.list().length);
songs.addAll(Arrays.asList(home.list()));
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.test_item, songs));
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
currentPosition = position;
playSong(MEDIA_PATH + songs.get(position));
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
// Setup listener so next song starts automatically
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private void nextSong() {
if (++currentPosition >= songs.size()) {
// Last song, just reset currentPosition
currentPosition = 0;
} else {
// Play next song
playSong(MEDIA_PATH + songs.get(currentPosition));
}
}
}
Another supporting class is,
package com.Chandra.TestMedia;
import java.io.File;
import java.io.FilenameFilter;
public class Mp3Filter implements FilenameFilter
{
@Override
public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return true;
}
}
And my manifest is as follows,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chandra.TestMedia"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestMediaActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And I am testing this on HTC wildfire which have 2.2.1 os and I implemented the app in 2.2.
My problem is I got crash when I try to print the length,System.out.println("home.list().length " + home.list().length); I am sure there are so many music files in my device. Could you guys please help what I did wrong?
Erro log is,
08-16 12:19:39.028: E/AndroidRuntime(519): FATAL EXCEPTION: main
08-16 12:19:39.028: E/AndroidRuntime(519): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vsoft.TestMedia/com.vsoft.TestMedia.TestMediaActivity}: java.lang.NullPointerException
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.os.Looper.loop(Looper.java:123)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-16 12:19:39.028: E/AndroidRuntime(519): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 12:19:39.028: E/AndroidRuntime(519): at java.lang.reflect.Method.invoke(Method.java:521)
08-16 12:19:39.028: E/AndroidRuntime(519): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-16 12:19:39.028: E/AndroidRuntime(519): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-16 12:19:39.028: E/AndroidRuntime(519): at dalvik.system.NativeStart.main(Native Method)
08-16 12:19:39.028: E/AndroidRuntime(519): Caused by: java.lang.NullPointerException
08-16 12:19:39.028: E/AndroidRuntime(519): at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
08-16 12:19:39.028: E/AndroidRuntime(519): at java.util.Arrays.asList(Arrays.java:171)
08-16 12:19:39.028: E/AndroidRuntime(519): at com.vsoft.TestMedia.TestMediaActivity.updateSongList(TestMediaActivity.java:40)
08-16 12:19:39.028: E/AndroidRuntime(519): at com.vsoft.TestMedia.TestMediaActivity.onCreate(TestMediaActivity.java:34)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 12:19:39.028: E/AndroidRuntime(519): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-16 12:19:39.028: E/AndroidRuntime(519): ... 11 more
Thanks, Chandra
Upvotes: 1
Views: 8109
Reputation: 1670
First create a ContentResolver
instance, retrieve the URI
for external music files, and create a Cursor instance using the ContentResolver
instance to query the music files:
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
Now you can iterate over the results:
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songList.add(new Song(thisId, thisTitle, thisArtist));
}
while (musicCursor.moveToNext());
}
Upvotes: 1
Reputation: 291
You can use the MediaStore content provider to find your music: http://developer.android.com/reference/android/provider/MediaStore.html.
There's a DATA field in the MediaColumns that holds the data stream for the media file.
Google has an example of reading images from the MediaStore http://developer.android.com/guide/topics/providers/content-providers.html.
Pretty much everything external to your app can usually be accessed using a Content Provider.
Upvotes: 2