2

在我的应用程序中,我使用这种方法来访问音频文件:

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;
    }
}
4

5 回答 5

4

我认为你输入了错误的uri。在这个 Uri 上没有媒体文件,这就是你得到空指针异常的原因。按照以下链接
获取媒体信息

于 2012-10-29T06:32:42.040 回答
0

检查变量dostawca,它看起来没有初始化。这就是为什么你在这一行得到空指针异常Cursor tablicaPlikow = dostawca.pobierzPlikiAudio();

于 2012-10-29T07:51:19.110 回答
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

此代码与我一起使用.. 如果您有任何问题,请随时提问。

于 2012-10-29T06:43:56.370 回答
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();
    }
}
于 2015-05-13T08:32:37.210 回答
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;
}
于 2016-01-05T12:46:40.917 回答