在我的应用程序中,我正在使用此方法来访问音频文件:
public Cursor pobierzPlikiAudio(){
String[] STAR = {"*"};
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Uri uri= Uri.parse("content://pl/aplikacja/app/provider/MediaStore/Audio");
return getContentResolver().query(uri, STAR, selection, null, null);
}
但在我的LogCat中,我得到一个nullPointerException。有什么问题?
Logcat出错:
10-29 07:04:17.729: E/AndroidRuntime(24354): FATAL EXCEPTION: main
10-29 07:04:17.729: E/AndroidRuntime(24354): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.aplikacja.app/pl.aplikacja.app.MainActivity}: java.lang.NullPointerException
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.os.Looper.loop(Looper.java:137)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-29 07:04:17.729: E/AndroidRuntime(24354): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 07:04:17.729: E/AndroidRuntime(24354): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 07:04:17.729: E/AndroidRuntime(24354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-29 07:04:17.729: E/AndroidRuntime(24354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-29 07:04:17.729: E/AndroidRuntime(24354): at dalvik.system.NativeStart.main(Native Method)
10-29 07:04:17.729: E/AndroidRuntime(24354): Caused by: java.lang.NullPointerException
10-29 07:04:17.729: E/AndroidRuntime(24354): at pl.aplikacja.app.MainActivity.onCreate(MainActivity.java:18)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.Activity.performCreate(Activity.java:4465)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-29 07:04:17.729: E/AndroidRuntime(24354): ... 11 more
这是MainActivity.java
package pl.aplikacja.app;
import android.os.Bundle;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.provider.MediaStore;
public class MainActivity extends Activity {
private Dostawca dostawca;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor tablicaPlikow = dostawca.pobierzPlikiAudio();
if (tablicaPlikow.moveToFirst()) {
String title;
String artist;
String album;
int titleColumn = tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_TITLE);
int artistColumn = tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_ARTIST);
int albumColumn =tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_ALBUM);
// Get the field values
title = tablicaPlikow.getString(titleColumn);
artist = tablicaPlikow.getString(artistColumn);
album = tablicaPlikow.getString(albumColumn);
TextView widok = (TextView) findViewById(R.id.tekst);
widok.setText(""+title+" "+ artist + " " + album);
}
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:4)
我认为你错了uri。在这个Uri上没有媒体文件,这就是为什么你得到空指针异常。请点击以下链接
GetMedia Information
答案 1 :(得分:0)
以下是一些获取所有音频并以列表格式显示的代码。
mAdapter = new SimpleCursorAdapter(
this,
// Use a template that displays a text view
R.layout.media_select_row,
// Give the cursor to the list adatper
createCursor(""),
// Map from database columns...
new String[] {
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
//MediaStore.Audio.Media.DATE_ADDED,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media._ID},
// To widget ids in the row layout...
new int[] {
R.id.row_artist,
R.id.row_album,
R.id.row_title,
R.id.row_icon,
R.id.row_options_button});
setListAdapter(mAdapter);
getListView().setItemsCanFocus(true);
// Normal click - open the editor
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent,
View view,
int position,
long id) {
DoWhatYouWant();
}
});
} catch (SecurityException e) {
// No permission to retrieve audio?
Log.e("RecordingApp", e.toString());
// todo error 1
} catch (IllegalArgumentException e) {
// No permission to retrieve audio?
Log.e("RecordingApp", e.toString());
// todo error 2
此代码与我合作..如果您有任何疑问,请随时提出。
答案 2 :(得分:0)
检查变量dostawca
它看起来没有初始化。这就是为什么你在这一行Cursor tablicaPlikow = dostawca.pobierzPlikiAudio();
答案 3 :(得分:0)
使用此代码
public void getAllSongsFromSDCARD()
{
String[] STAR = { "*" }; //it is projection you can modify it according to your need
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
String[] selectionArgs = null;
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; // check for music files
cursor = getContentResolver().query(allsongsuri,STAR,selection,selectionArgs,sortOrder );
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
}
}
答案 4 :(得分:0)
使用这个
public static ArrayList<MediaItem> listOfSongs(Context context) {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Filter only mp3s, only those marked by the MediaStore to be music and longer than 1 minute
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
// +" AND " + MediaStore.Audio.Media.MIME_TYPE + "= 'audio/mpeg'"
// `+ " AND " + MediaStore.Audio.Media.DURATION + " > 60000";
String sortOrder = MediaStore.Audio.AudioColumns.TITLE
+ " COLLATE LOCALIZED ASC";
Cursor c = context.getContentResolver().query(uri, null, selection, null, sortOrder);
ArrayList<MediaItem> listOfSongs = new ArrayList<MediaItem>();
try{
c.moveToFirst();
while (c.moveToNext()) {
MediaItem songData = new MediaItem();
String title = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long duration = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
long albumId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String composer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.COMPOSER));
songData.setTitle(title);
songData.setAlbum(album);
songData.setArtist(artist);
songData.setDuration(duration);
songData.setPath(data);
songData.setAlbumId(albumId);
songData.setComposer(composer);
listOfSongs.add(songData);
}
c.close();
Log.d("SIZE", "SIZE: " + listOfSongs.size());
}catch (Exception e){
Log.d(TAG,"listOfSongs() "+e.getMessage());
}
return listOfSongs;
}