
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();
}
}
When I compiled this code, got the following error as:
ReplyDelete/****
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
What if i want to list video files stored in internal storage???
ReplyDeleteI used your code to playing video,but i m facing some problem regarding video that is picture is not comming when video is playing.
ReplyDeleteI will thankfull to you. If you respones.
how to see video in android
ReplyDeleteDears,
ReplyDeleteI 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?
Hey,
ReplyDeleteI 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
Hi ..
ReplyDeleteI want write the current plating video name on the SeekBar.I s it possible to do?
hai friend now i am try your code but its not work
ReplyDeleteits indicate sorry! the application video007(process video.video)has stopped unexpectedly. please try again.
what mistake i made tell me...
ReplyDeletesuperb tutorial. the code simply worked greatly
ReplyDeletethx 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
ReplyDeletethe code is running without error bt list is nt displayed. Whats the matter?
ReplyDeleteWill please tell me file path to palace the video files
ReplyDeleteHi,
ReplyDeleteCan you give some information on SD card as to where and how the videos are saved priorly?
Waiting for your kind reply.
Thanks,
Alex
when i install apk in emulator 2.0.this code is not working on emulator 2.0...
ReplyDeleteit shows "sorry,this video cannot played"
anyone can helps...!
hi nice tutorial
ReplyDeletei 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?
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.
ReplyDeleteThis tutorial is nice one but how to play large data video android.Can i able to mediaplayer for play video in android...
ReplyDeletePlease reply me.
Thanks,
i havent run this, but you never add the content of the cursor to the listadapter.... no list will show..
ReplyDeleteoh 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
ReplyDeleteplease let me know where to download the code for this project.
ReplyDeletei have a page with 3 videos and all videos are link to server.How can i open this video and play.
ReplyDeletePlase help me
my email id is anup.khunteta@gmail.com
Somebody knows how can i put a print for the video in this list?
ReplyDeleteI 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
ReplyDeleteThe tutorial is very nice. But in this i want to display image thumbnails,how to do this? Then Display only sdcard videos
ReplyDeleteAdvance thanks
i have an error on line
ReplyDeletethat is, error parsing xml not well formed (invalid token)
anyone send replay to my mail prathap.malempati@gmail.com
Thanks its working well but only songs name is view not songs thumbnail
ReplyDeletebut again thanks