android从sdcard读取文件

时间:2014-11-18 10:36:37

标签: android file mp3

我正在构建一个Android音乐播放器,它出现在Android模拟器上但后来停止但是当我在Android设备上尝试它时效果很好。我正在尝试阅读手机sdcard上的音乐列表。

这是错误

11-18 10:27:36.630: W/dalvikvm(3148): threadid=19: thread exiting with uncaught exception (group=0xb1af7d70)
11-18 10:27:36.650: E/AndroidRuntime(3148): *** FATAL EXCEPTION IN SYSTEM PROCESS: android.display
11-18 10:27:36.650: E/AndroidRuntime(3148): java.lang.OutOfMemoryError
11-18 10:27:36.650: E/AndroidRuntime(3148):     at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.content.res.Resources.loadDrawable(Resources.java:2115)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.content.res.Resources.getDrawable(Resources.java:700)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at com.android.internal.widget.ActionBarView.setIcon(ActionBarView.java:699)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3201)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1695)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at com.android.internal.policy.impl.PhoneWindowManager.addStartingWindow(PhoneWindowManager.java:1743)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7248)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.os.Looper.loop(Looper.java:136)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at android.os.HandlerThread.run(HandlerThread.java:61)
11-18 10:27:36.650: E/AndroidRuntime(3148):     at com.android.server.ServiceThread.run(ServiceThread.java:46)

我的机器人清单

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nn"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
        android:minSdkVersion="9"
 />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/mainicon"
        android:label="@string/app_name"
             android:theme="@style/AppTheme"
          >
        <activity
            android:name=".Open"
            android:label="@string/app_name"
           >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      <activity
            android:label="list"
            android:name=".MainActivity"></activity>"      
                  <activity
            android:label="list"
            android:name=".Play"></activity>"      
           <activity
            android:label="songs"
            android:name=".Bame"></activity>" 
    </application>

</manifest>

这是我的java类

package com.example.nn;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;




import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class Bame extends ListActivity {

 protected static final String PREFS_NAME = null;
String info; 
        ArrayList<String> songs = new ArrayList<String>();
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        Uri uri;
        MediaPlayer mp;
        int columnIndex;
        String[] projection = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ALBUM_ID };
        ListView lst;
        ArrayAdapter<String> adapter;
        Cursor cursor;

        public void onCreate(Bundle savedInstanceState) {
               requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                        WindowManager.LayoutParams.FLAG_FULLSCREEN);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.playlist);

            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

            cursor = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    projection, selection, null, null);


            while(cursor.moveToNext()){
                songs.add(cursor.getString(0));
                songs.add(cursor.getString(1));
                songs.add(cursor.getString(2));
                songs.add(cursor.getString(3));
                songs.add(cursor.getString(4));
                songs.add(cursor.getString(5));


        } 
                int a[]= new int[]{R.id.textView1 ,R.id.textView2};
                final ListAdapter adapter = new SimpleCursorAdapter(this,
               R.layout.spin, cursor, new String[]{MediaStore.Audio.Media.TITLE, 
                        MediaStore.Audio.Media.ARTIST} ,a);
                setListAdapter(adapter); 
                ListView lv = getListView();    
                lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View viewClicked,
                            int position, long id) {
                    cursor= (Cursor) adapter.getItem(position);
                    cursor.moveToPosition(position);
                      int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                     int sec = (duration/1000)%60;
                     int min = (duration/1000)/60;
            info= "msgMediaPlayer" + "\n"

                                 + "DURATION: "+min +":"+ sec ;
            int id1=cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
            Toast.makeText(Bame.this,id1, Toast.LENGTH_LONG).show();

                        }                
                });
            }

        }

1 个答案:

答案 0 :(得分:0)

这只是形象问题。我缩小了图像的大小,这就是全部。