I am Using this Fragment in which Song title,Artist Name, and Album art for corresponding song is being displayed in the custom ListView.
I am getting smooth Scrolling with the code. But the problem is that memory consumption for the app containing only one Activity and Fragment is very high. Can you suggest alternative approach. or help me increase efficiency of this code. thanks.
package com.vamp.playerFragments;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.vamp.R;
import com.vamp.adapters.AllSongsAdapter;
import com.vamp.models.AllSongsModel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AllSongs extends Fragment {
public AllSongsAdapter adapter;
private ListView allSongsList;
private int[] albumArts;
private String[] allSongs;
private String[] artistName;
private ArrayList<AllSongsModel> rowItems;
private OnFragmentInteractionListener mListener;
public AllSongs() {
// Required empty public constructor
}
public static AllSongs newInstance(String param1, String param2) {
AllSongs fragment = new AllSongs();
Bundle args = new Bundle();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_all_songs, container, false);
allSongsList = (ListView) rootView.findViewById(R.id.allSongsList);
rowItems = new ArrayList<AllSongsModel>();
init_Songs_list();
adapter = new AllSongsAdapter(getActivity(), rowItems);
allSongsList.setAdapter(adapter);
return rootView;
}
public void init_Songs_list() {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String proj[] = {
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID
};
Cursor MainCursor = getActivity().getContentResolver().query(uri, proj, MediaStore.Audio.Media.IS_MUSIC + " =1", null, MediaStore.Audio.Media.TITLE + " ASC");
MainCursor.moveToFirst();
while (MainCursor.moveToNext()) {
String title = MainCursor.getString(MainCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String artistName = MainCursor.getString(MainCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
Long albumId = MainCursor.getLong(MainCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Uri sArtWorkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtWorkUri, albumId);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(
getActivity().getContentResolver(), albumArtUri);
//bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);
} catch (FileNotFoundException exception) {
exception.printStackTrace();
bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
R.drawable.ic_launcher);
} catch (IOException e) {
e.printStackTrace();
}
AllSongsModel allSongsModel = new AllSongsModel();
allSongsModel.setSongName(title);
allSongsModel.setArtistName(artistName);
allSongsModel.setAlbumArt(bitmap);
rowItems.add(allSongsModel);
}
}
}