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



}


Popular posts from this blog

Custom Volley-Gson request implementation

This page covers Volley-Gson integration and also Volley custom Request. We assume that, you are familiar with Volley and Gson.

Volley has some inbuilt request which you can use, if your response is a String, Image, or JSON, in that case, you don't need to implement a custom Request.  To use Gson with Volley, we need to implement a custom request.
To make a custom request, you need to perform following operation.
Extend Request<T> class, where T is the type of parsed response or expected response. Implement the abstract methods parseNetworkResponse() and deliverResponse().  parseNetworkResponse:  A response contains parse response of a given type i.e Gson.

@Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
                    response.data, HttpHeaderParser.parseCharset(response.headers));
            if (BuildConfig.IS_DEBUG)
                Log.d(TAG, "Response :: " + …

Display News and Videos through RSS and Display it in Listview

Hi friends , In this tutorial ,i'm going to describe about how to fetch data from rss feed.Here i had also problem ,when i was developing my recent project.I had also faced  many problem and at last i developed my best project.



Here is the code that describe how to fetch news headline from rss
This class describes how to parse XML data:
import java.net.URL;
import java.util.ArrayList;



import org.xmlpull.v1.XmlPullParser;



import android.content.res.XmlResourceParser;
import android.util.Log;
import android.util.Xml;



public class XmlFeedParser {

    //Feed Parsing Method
    public ArrayList<Implement> parse(String url) {

        //Array of Episode Objects
        ArrayList<Implement> episodes = null;

        try {
            //Encode the URL into a URL Object
            URL episode_feed_url = new URL(url);

            //Open a Connection to the feed
            XmlPullParser parser = Xml.newPullParser();
            try {
                parser.setInput(episode_feed_url.o…