Tuesday, June 16, 2009

Displaying the List of video files Stored in SD card and playing the video



The folowing example used to list all the video files stored in SDcard and you can
play the video by selecting the file from list.

AndroidManifest.xml
--------------------
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="sample.video" android:versionCode="1" android:versionName="1.0.0">
      < uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
      < application android:icon="@drawable/icon" android:label="@string/app_name">
            < activity android:name=".VideoActivity" 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:name=".ViewVideo">
                  < intent-filter>
                        < action android:name="android.intent.action.VIEW" />
                        < category android:name="android.intent.category.DEFAULT" />
                  </intent-filter>
            </activity>
      </application>
</manifest>


main.xml
--------
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      < ListView
            android:id="@+id/PhoneVideoList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
</LinearLayout>

VideoActivity.java
-------------------
package sample.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class VideoActivity extends Activity {
      private Cursor videocursor;
      private int video_column_index;
      ListView videolist;
      int count;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init_phone_video_grid();
      }

      private void init_phone_video_grid() {
            System.gc();
            String[] proj = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
            videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
            count = videocursor.getCount();
            videolist = (ListView) findViewById(R.id.PhoneVideoList);
            videolist.setAdapter(new VideoAdapter(getApplicationContext()));
            videolist.setOnItemClickListener(videogridlistener);
      }

      private OnItemClickListener videogridlistener = new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
long id) {
                  System.gc();
                  video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                  videocursor.moveToPosition(position);
                  String filename = videocursor.getString(video_column_index);
                  Intent intent = new Intent(VideoActivity.this, ViewVideo.class);
                  intent.putExtra("videofilename", filename);
                  startActivity(intent);
            }
      };

      public class VideoAdapter extends BaseAdapter {
            private Context vContext;

            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();
                  TextView tv = new TextView(vContext.getApplicationContext());
                  String id = null;
                  if (convertView == null) {
                        video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                        videocursor.moveToPosition(position);
                        id = videocursor.getString(video_column_index);
                        video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
                        videocursor.moveToPosition(position);
                        id += " Size(KB):" + videocursor.getString(video_column_index);
                        tv.setText(id);
                  } else
                        tv = (TextView) convertView;
                  return tv;
            }
      }
}



ViewVideo.java
---------------
package sample.video;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class ViewVideo extends Activity {
      private String filename;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            System.gc();
            Intent i = getIntent();
            Bundle extras = i.getExtras();
            filename = extras.getString("videofilename");
            VideoView vv = new VideoView(getApplicationContext());
            setContentView(vv);
            vv.setVideoPath(filename);
            vv.setMediaController(new MediaController(this));
            vv.requestFocus();
            vv.start();
      }
}

34 comments:

  1. When I compiled this code, got the following error as:
    /****
    getactualaacconfig.h'
    /bin/bash: line 0: cd: development/tools/layoutopt/app/src/resources: No such file or directory
    Prebuilt: out/debug/target/product/harmony/root/init.rc
    target R.java/Manifest.java: Video (out/debug/target/common/obj/APPS/Video_intermediates/src/R.stamp)
    packages/apps/Video/AndroidManifest.xml:2: error: Error parsing XML: not well-formed (invalid token)
    make[1]: *** [out/debug/target/common/obj/APPS/Video_intermediates/src/R.stamp] Error 1
    make[1]: Leaving directory `/home/ldewangan/mplayer/sw/mobile/linux/distro/android/eclair'
    make: *** [android_rebuild] Error 2

    if possible, can you please send me the tgz file?
    ***/
    Can you please help me on how to reolve this?

    My email-id:
    ldewangan@nvidia.com

    ReplyDelete
  2. What if i want to list video files stored in internal storage???

    ReplyDelete
  3. I used your code to playing video,but i m facing some problem regarding video that is picture is not comming when video is playing.

    I will thankfull to you. If you respones.

    ReplyDelete
  4. how to see video in android

    ReplyDelete
  5. Dears,

    I used your code to playing video. The behaviour is strange to me. The audio of the 3gp file is ok without problem. The video of the 3gp file just played as soon as frozen.

    I expected the video shall keep on playing.

    Can you give me a tip on this?

    ReplyDelete
  6. Hey,

    I am very new to Android

    btw. that's a great tut...as I have been looking for s.th. like for some time now..

    but I got a question... what would I need to do if I wanted to display the videos as thumbs?? I found the MediaStore.Video.Thumbnails class, but I am not sure how and where I should put it inside your code...If you could help me out that would be great...Thanks

    ReplyDelete
  7. Hi ..
    I want write the current plating video name on the SeekBar.I s it possible to do?

    ReplyDelete
  8. hai friend now i am try your code but its not work

    its indicate sorry! the application video007(process video.video)has stopped unexpectedly. please try again.

    ReplyDelete
  9. what mistake i made tell me...

    ReplyDelete
  10. superb tutorial. the code simply worked greatly

    ReplyDelete
  11. thx for this wonderful code it run perfectlty.i want to ask you one thing that if we want to also play the song of phone memory then wat changes are require in exiting code.plz let me know.my id is tanu.bca@gmail.com

    ReplyDelete
  12. the code is running without error bt list is nt displayed. Whats the matter?

    ReplyDelete
  13. Will please tell me file path to palace the video files

    ReplyDelete
  14. Hi,

    Can you give some information on SD card as to where and how the videos are saved priorly?

    Waiting for your kind reply.

    Thanks,
    Alex

    ReplyDelete
  15. when i install apk in emulator 2.0.this code is not working on emulator 2.0...
    it shows "sorry,this video cannot played"
    anyone can helps...!

    ReplyDelete
  16. hi nice tutorial
    i saved 3 video files in sdcard, code works fine, but when i saved other .3gp videos to sdcard these files are not available in list view.
    anyone can tell me how that can be achieved?

    ReplyDelete
  17. this works fine. i need to populating video with its thumb nail in a listview from urls. how to do that . please include code for that.it may great to me and all.

    ReplyDelete
  18. This tutorial is nice one but how to play large data video android.Can i able to mediaplayer for play video in android...


    Please reply me.

    Thanks,

    ReplyDelete
  19. i havent run this, but you never add the content of the cursor to the listadapter.... no list will show..

    ReplyDelete
  20. oh my mistake, you don't fill the base adapter at all.. you leave the cursor open which is not a good idea. you should propagate through the cursor filling the adapter quickly, then close the cursor, fyi

    ReplyDelete
  21. please let me know where to download the code for this project.

    ReplyDelete
  22. i have a page with 3 videos and all videos are link to server.How can i open this video and play.

    Plase help me
    my email id is anup.khunteta@gmail.com

    ReplyDelete
  23. Somebody knows how can i put a print for the video in this list?

    ReplyDelete
  24. I want to make custom list which has two textview and imageview how i set thumbnail and name of file and size on that layout with custom adapter Plz reply

    ReplyDelete
  25. The tutorial is very nice. But in this i want to display image thumbnails,how to do this? Then Display only sdcard videos

    Advance thanks

    ReplyDelete
  26. i have an error on line


    that is, error parsing xml not well formed (invalid token)

    anyone send replay to my mail prathap.malempati@gmail.com

    ReplyDelete
  27. Thanks its working well but only songs name is view not songs thumbnail
    but again thanks

    ReplyDelete
  28. Thanks a lot..its perfect

    ReplyDelete
  29. Not working properly...a particular video from list is clicked and some other video plays!!!

    ReplyDelete
  30. When I use this Its give file not found exception

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. it will show list of video from sdcard but not playing the video when click on it....

    ReplyDelete