Hi friends ,This is complete
post, about how to display video form specific folder and display it
with rounded shape and play it on click event.
For Rounded shape I have used custom
ImageView.Now have a look on code part.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
gridlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<com.nexbits.video.RoundedImageView
android:id="@+id/grid_item_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="test string"
android:textColor="#00ffff"
android:textSize="10dip" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nexbits.video"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".VideoGriedActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Java Code:
VideoGriedActivity.java
package com.dilip.video;
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class VideoGriedActivity extends Activity {
private static final String MEDIA_PATH = new String(
"/mnt/sdcard/FreedomMic/test");
String[] fileList = null;
GridView gridView;
String FILE_PATH = "/mnt/sdcard/Freedommic/test/";
String MiME_TYPE = "video/mp4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateSongList();
gridView = (GridView) findViewById(R.id.gridView1);
if (fileList != null) {
gridView.setAdapter(new ImageAdapter(this, fileList));
}
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.grid_item_label))
.getText(), Toast.LENGTH_SHORT).show();
String videoFilePath = FILE_PATH + fileList[position];
System.out
.println("******************************videoFilePath****************"
+ videoFilePath);
System.out
.println("******************************MiME_TYPE****************"
+ MiME_TYPE);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File newFile = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(newFile), MiME_TYPE);
startActivity(intent);
}
});
}
public void updateSongList() {
File videoFiles = new File(MEDIA_PATH);
Log.d("*********Value of videoFiles******", videoFiles.toString());
if (videoFiles.isDirectory()) {
fileList = videoFiles.list();
}
if (fileList == null) {
System.out.println("File doesnot exit");
Toast.makeText(this, "There is no file", Toast.LENGTH_SHORT).show();
} else {
System.out.println("fileList****************" + fileList);
for (int i = 0; i < fileList.length; i++) {
Log.e("Video:" + i + " File name", fileList[i]);
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] VideoValues;
public ImageAdapter(Context context, String[] VideoValues) {
this.context = context;
this.VideoValues = VideoValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("***********IngetView************");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from gridlayout.xml
gridView = inflater.inflate(R.layout.gridlayout, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(fileList[position]);
System.out.println("value of fileList[position]" + fileList[0]);
// set image
ImageView imageThumbnail = (ImageView) gridView
.findViewById(R.id.grid_item_image);
Bitmap bmThumbnail;
System.out
.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> file path>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
+ fileList[position]);
bmThumbnail = ThumbnailUtils.createVideoThumbnail(FILE_PATH
+ fileList[position],
MediaStore.Video.Thumbnails.MINI_KIND);
if (bmThumbnail != null) {
System.out
.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
imageThumbnail.setImageBitmap(bmThumbnail);
} else {
System.out
.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>NO THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
} else {
gridView = (View) convertView;
}
return gridView;
}
public int getCount() {
// return 0;
return VideoValues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
}
Android I have used RoundedImageView.java class for custom ImageView
RoundedImageView.java
package com.dilip.video;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
super(context);
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
float radius = 25.0f;
Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
And finally Output is all follows:-
}
Hi Dilip Nice tutorial, but how to select videos only, right now it selects all video and image in the folder
ReplyDeleteif you want to use this code then you'll have to filter your videos or you can fire query for get videos also.
DeleteHi,i am new for android development.please can you suggest me
ReplyDeletehow i start android development from basic.
You should start from Android Documentation.This is well documented.Follow this link http://developer.android.com/training/basics/firstapp/index.html
Deletehi great work!!!
ReplyDeletei have implemented this, but i'm facing a small issue in it.
when i click on the Rounded Image thumb view video, it says sorry, this video cannot be played.
how to resolve this?
check your player and also print video file before giving video path to Player.may be your video path is not correct or Player doesn’t support this type.
Delete