Skip to main content

Display video from specific folder with rounded shape thumbnail

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:-



}


Comments

  1. Hi Dilip Nice tutorial, but how to select videos only, right now it selects all video and image in the folder

    ReplyDelete
    Replies
    1. if you want to use this code then you'll have to filter your videos or you can fire query for get videos also.

      Delete
  2. Hi,i am new for android development.please can you suggest me
    how i start android development from basic.

    ReplyDelete
    Replies
    1. You should start from Android Documentation.This is well documented.Follow this link http://developer.android.com/training/basics/firstapp/index.html

      Delete
  3. hi great work!!!

    i 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?

    ReplyDelete
    Replies
    1. 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

Post a Comment

Popular posts from this blog

Android O New Features Overview

This post assumes, you are an experienced developer who wants to get started with the latest version of Android. Android O is not a huge update to the OS and application framework like M but it still has many useful features for both developer and an user. Android O has focus on below areas. Notification redesigned Picture-in-Picture(PIP)  Visual adaption for different devices  Battery life improved Setting app reorganized Notification redesigned: Android O notification changes includes more easy and manageable way to manager your notification behavior and settings. It includes: Notification Channel: Notification channel allows you to create user customizable channel for each type of notification you wanna display. A single application can have multiple channel, a separate channel for each type of notification you wanna display. Having said this, you can create s separate channel for audio & image notification. User can disable specific notification channel instead

Java 8 Overview

Java 1.8 has introduced major features for the Java developers. It includes various upgrade to the Java programming, JVM, Tools and libraries. The main purpose of Java 1.8 release has been to simplify programming, utilize functional programming benefits and enable parallel programming/processing in Java programming language. Java 1.8 feature  Lambda(->) Expression Functional interfaces Default Methods in interface static method inside interface     Pre defined functional interfaces Predicate Function Consumer ::(Method reference and constructor reference by using double colon(::)operator) Stream API Date & Time API

Overview of how to develop native code with Android NDK:

  Create a jni directory and place native source code under $PROJECT/jni/... Create Android.mk directory and place it under $PROJECT/jni/... to describe source code to the NDK build system. Create Application.mk and place it under $PROJECT/jni/...to describe project in more details to the NDK build system.It is an optional. Finally need to build native source code by running '$NDK/ndk-build' from project directory or it's sub-directory. Android.mk An Android.mk file is a small build script that we write to describe sources to the NDK build system. It's syntax is like this... LOCAL_PATH:=$(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE:=hello-jni LOCAL_SRC_FILES:=hello-jni.c include $(BUILD_SHARED_LIBRARY) NDK groups your sources into "modules", where each module can be one of the following: Static library Shared library We can write several module in a single 'Android.mk' or can write seve