2012-10-29 92 views
2

在我的應用程序正在使用此方法來訪問音頻文件:Android:我怎樣才能訪問音頻文件?

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

你能後的logcat,登錄你在哪裏得到的錯誤越來越空指針exceptin。 –

+0

檢查更新後的答案 –

回答

4

,我認爲你是把錯誤的URI。在這個Uri沒有媒體文件,這就是爲什麼你得到空指針異常。請按照以下鏈接
GetMedia Information

+0

我將Uri更改爲「MediaStore.Audio.Media.EXTERNAL_CONTENT_URI」,但問題仍然存在 –

+0

我認爲您正在模擬器上運行此代碼。所以請確保在您的仿真器SD卡中創建了一些示例音樂文件。 –

+0

不,我在我的手機上運行這個代碼,其中包含SD卡。 –

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 

此代碼與我合作..如果您有任何問題,請隨時問。

0

檢出變量dostawca看起來它沒有被初始化。這就是爲什麼你在這行Cursor tablicaPlikow = dostawca.pobierzPlikiAudio();

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(); 
    } 
} 
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; 
}