Question

hi guys i am making an android application IN which i have to display all the video contents of the sd card (album wise).....it means when WE WILL LOAD THE APPLICATION only the names of albums should be displayED.... AND when we click on the particular album ITS VIDEOS FILES SHOULD BE DISPLAYED................... i have been able to display the album names BUT THE PROBLEM IS THAT there is duplicate album names (i.e. IF TWO VIDEO files are of same album) then two different albums are being displaying...........so i want all the videos of same album should be in one "album name"........... and on the click of that album name "all its video contents should be displayed" any help will be appreciated below is the main class (AlbumVideo.class)

       public class AlbumVideo extends Activity  {
  private Cursor audiocursor;
  private int audio_column_index;
  ListView audiolist;
  int count;
  int album=1;

  int dura;
  int i1 =R.drawable.ic_launcher;


  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imagexml1);

        init_phone_videos_grid();


  }

  @SuppressWarnings("deprecation")
private void init_phone_videos_grid() {
        System.gc();
        final String[] projection = new String[] { 
                MediaStore.Video.VideoColumns.ALBUM };
        final String sortOrder = VideoColumns.ALBUM + " COLLATE LOCALIZED ASC";
      audiocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                                    projection, null, null, sortOrder);


        count = audiocursor.getCount();
       audiolist = (ListView) findViewById(R.id.PhoneVideo);
        audiolist.setAdapter(new AudioAdapter(getApplicationContext()));

      audiolist.setOnItemClickListener(videogridlistener);

  }
  private OnItemClickListener videogridlistener = new OnItemClickListener() {
        @SuppressWarnings("rawtypes")
        public void onItemClick(AdapterView parent, View v, int position,

  long id) {

              System.gc();
              audio_column_index = audiocursor

                        .getColumnIndexOrThrow(MediaStore.Video.VideoColumns.ALBUM);
                                     audiocursor.moveToPosition(position);
                                      String filename = audiocursor.getString(audio_column_index); 
                                      Intent intent = new Intent(AlbumVideo.this,
                                               AlbumVideoDetail.class);
                                               intent.putExtra("albumfilename",filename);
                                               intent.putExtra("album",album);

                                               startActivity(intent);

        }
  };




  public class AudioAdapter extends BaseAdapter 
  {
        private Context vContext;
        CheckBox cb;
        int position=0;
        int check=0;
        String a[]=new String[count];

        public AudioAdapter(Context c) {
              vContext = c;
        }

        public int getCount() {
              return count;
        }

        public Object getItem(int position) {
              return position;
        }

        public long getItemId(int position) {
              return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
              System.gc();
              LayoutInflater inflater = getLayoutInflater();

              View row;

              row = inflater.inflate(R.layout.list_item1, parent, false);

              final Button title = (Button) row.findViewById(R.id.button);

              title.setTextColor(Color.parseColor("#000000"));

              if (convertView == null) {

                  audio_column_index =audiocursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.ALBUM );

                  audiocursor.moveToPosition(position);
                   String   TITLE = audiocursor.getString(audio_column_index);
                   String a[]=new String[count];
                   Toast.makeText(AlbumVideo.this, "music will be available shortly"+TITLE, Toast.LENGTH_LONG).show();




                  title.setText(TITLE);

              }

              return (row);  
        }


  }

}

and the second class to display the songs of the particular album is ("AlbumVideoDetail.class")

        public class AlbumVideoDetail extends Activity  {
  private Cursor videocursor;
  private int video_column_index;
  ListView videolist;
  int count;
  String title=null;
  String desc=null;
  int dura;
  int i1 =R.drawable.ic_launcher;
  String filename;


  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imagexml);
        Intent i = getIntent();
        Bundle extras = i.getExtras();
   filename = extras.getString("albumfilename");
//     String filename1 = filename.substring(filename.indexOf("sdcard"),filename.length());
  //   String filename2= filename1.substring(7);
     Toast.makeText(AlbumVideoDetail.this, "music will be available shortly"+filename, Toast.LENGTH_LONG).show();
        init_phone_videos_grid();


  }

  @SuppressWarnings("deprecation")
private void init_phone_videos_grid() {
        System.gc();
        String[] proj = { MediaStore.Video.Media.DATA,
                MediaStore.Video.Media._ID, MediaStore.Video.Media.TITLE,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.MIME_TYPE,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.SIZE,};
        String where = android.provider.MediaStore.Video.Media.ALBUM + "=?";
        String whereVal[] = { filename };
        String orderBy = android.provider.MediaStore.Video.Media.TITLE;





       videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                proj, where, whereVal, orderBy);



        count = videocursor.getCount();
      videolist = (ListView) findViewById(R.id.PhoneVideoList);
      videolist.setAdapter(new VideoAdapter(getApplicationContext()));


  }


  public class VideoAdapter extends BaseAdapter {
        private Context vContext;
       CheckBox cb;
       int position=0;
     int check=0;

        public VideoAdapter(Context c) {
              vContext = c;
        }

        public int getCount() {
              return count;
        }

        public Object getItem(int position) {
              return position;
        }

        public long getItemId(int position) {
              return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
              System.gc();
              LayoutInflater inflater = getLayoutInflater();

              View row;

              row = inflater.inflate(R.layout.list_item2, parent, false);
              final TextView title = (TextView) row.findViewById(R.id.title);
              TextView desc = (TextView) row.findViewById(R.id.desc);
              TextView dura = (TextView) row.findViewById(R.id.duration);






              title.setTextColor(Color.parseColor("#000000"));
              desc.setTextColor(Color.parseColor("#000000"));
              dura.setTextColor(Color.parseColor("#000000"));





              if (convertView == null) {

                 video_column_index =videocursor

 .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                   videocursor.moveToPosition(position);
                 final String   TITLE = videocursor.getString(video_column_index);
                 title.setText(TITLE);



              video_column_index = videocursor

  .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
                    videocursor.moveToPosition(position);
                    String DESC= videocursor.getString(video_column_index);
                    int desd=Integer.parseInt(DESC);
                    int kb = (int) ((desd / 1000) % 1000);
                      int mb = (int) ((desd / 1000) / 1000);
                      String KB=Integer.toString(kb);
                      String MB=Integer.toString(mb);
                      String DESC1 = MB + "." + KB +"mb";

                  desc.setText(DESC1);   
                  video_column_index = videocursor

                          .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
                                            videocursor.moveToPosition(position);
                                          String DURA=  videocursor.getString(video_column_index);
                                          int duro = Integer.parseInt(DURA);
                                          int seconds = (int) ((duro / 1000) % 60);
                                          int minutes = (int) ((duro / 1000) / 60);
                                          String secs=Integer.toString(seconds);
                                          String mins=Integer.toString(minutes);
                                          String DURA1 = mins + ":" + secs;


                                          dura.setText(DURA1);
                                          video_column_index = videocursor

                                        .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                                                      videocursor.moveToPosition(position);

        final  String filename = videocursor.getString(video_column_index); 


         } return (row);  
 }


  }
       }

No correct solution

OTHER TIPS

wow after so much time spent on the internet at last found the solution for it !!!!!!! it an easy n very short step .,][.....i hope it will be helpful to someone ..........

      private void init_phone_videos_grid() {
    System.gc();
    String[] proj = { MediaStore.Video.Media.DATA,
            MediaStore.Video.Media._ID, MediaStore.Video.Media.TITLE,
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.MIME_TYPE,
            MediaStore.Video.Media.DURATION,
            MediaStore.Video.Media.SIZE,};
    String where = android.provider.MediaStore.Video.Media.ALBUM + "=?";
    String whereVal[] = { filename };
    String orderBy = android.provider.MediaStore.Video.Media.TITLE;





   videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "DISTINCT "
            +proj, where, whereVal, orderBy);



    count = videocursor.getCount();
  videolist = (ListView) findViewById(R.id.PhoneVideoList);
  videolist.setAdapter(new VideoAdapter(getApplicationContext()));

}

just to add the word "distinct" in the class "album detail"....n it will sort d query according to yourt requirement....

Change the following:

final String[] projection = new String[] {MediaStore.Video.VideoColumns.ALBUM };

into

final String[] projection = new String[]{ "DISTINCT " +MediaStore.Video.VideoColumns.ALBUM};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top