想要从SD卡

时间:2018-01-06 18:27:56

标签: android android-fragments android-mediaplayer

在我的音乐应用程序中,我想从SD卡获取歌曲专辑。我试了一下但是得不到这个。我如何根据专辑,艺术家和流派获得歌曲?和从SD卡上获取歌曲一样吗?

专辑活动

public class BlankFragment extends Fragment {
    ArrayList<albumInfo>album=new ArrayList<albumInfo>();


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;




    public BlankFragment() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView= inflater.inflate(R.layout.fragment_blank, container, false);
        ContentResolver contentResolver =getContext().getContentResolver();
        Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int albumId = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                long thisalbumId = mediaCursor.getLong(albumId);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);

                // Add the info to our array.
                if(thisId == thisalbumId)
                {
                    Log.wtf("SAME2SAME", String.valueOf(thisalbumId));
                    Log.wtf("SAME2SAME", String.valueOf(thisId));
                    album.add(new albumInfo(thisId, thisTitle, thisArtist));
                }
            }
            while (mediaCursor.moveToNext());

            // For best practices, close the cursor after use.
            mediaCursor.close();
        }

        return rootView;

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

这是专辑适配器

public class albumAdapter extends RecyclerView.Adapter<albumAdapter.AlbumHolder> {
    ArrayList<albumInfo>album=new ArrayList<>();
    Context context;

    public albumAdapter(ArrayList<albumInfo> album, Context context) {
        this.album = album;
        this.context = context;
    }

    @Override
    public albumAdapter.AlbumHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View myView = LayoutInflater.from(context).inflate(R.layout.album,parent,false);
        return new AlbumHolder(myView);
    }

    @Override
    public void onBindViewHolder(albumAdapter.AlbumHolder holder, int position) {

        final albumInfo a = album.get(position);
        holder.albumName.setText(album.get(position).getaName());
        holder.songsNo.setText(album.get(position).getNoOfSongs());

    }

    @Override
    public int getItemCount() {
        return album.size();
    }

    public class AlbumHolder extends RecyclerView.ViewHolder {
        public TextView songsNo;
        public TextView albumName;
        public AlbumHolder(View itemView) {
            super(itemView);
            albumName=(TextView)itemView.findViewById(R.id.albumName);
            songsNo=(TextView)itemView.findViewById(R.id.noOfSongs);

        }
    }
}

0 个答案:

没有答案