Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import android.provider.MediaStore;

public class Main {
    private static String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA };
    private static String[] mediaColumns = { MediaStore.Video.Media._ID };

    public static String getThumbnailPathForLocalFile(Context context, Uri fileUri) {

        long fileId = getFileId(context, fileUri);
        MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), fileId,
                MediaStore.Video.Thumbnails.MICRO_KIND, null);

        Cursor thumbCursor = null;
        try {
            thumbCursor = context.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                    thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + " = " + fileId, null, null);

            if (thumbCursor.moveToFirst()) {
                String thumbPath = thumbCursor
                        .getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                return thumbPath;
            }

        } finally {
        }

        return null;
    }

    private static long getFileId(Context context, Uri fileUri) {

        Cursor cursor = context.getContentResolver().query(fileUri, mediaColumns, null, null, null);

        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
            int id = cursor.getInt(columnIndex);
            return id;
        }

        return 0;
    }
}