Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image from video file

Tags:

android

image

Hi i'm developer in korea. I have some question so i enter this site.

    InputStream is;
    URL url = 
         new URL("http://112.216.25.58:8888/VOD_LAUNCHER/media/youtube_sample3.mp4");
    Uri uri = Uri.parse(url.toURI().toString());
    is = getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    //Bitmap bitmap = BitmapFactory.decodeFile(url.toString());
    //MediaMetadataRetriever ret = new MediaMetadataRetriever();
    //ret.setDataSource(url.toString());
    //Bitmap bitmap = ret.getFrameAtTime(0);
    //mImageView.setImageURI(uri);

    //Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(uri.toString(), Thumbnails.MICRO_KIND);
    mImageView.setImageBitmap(bitmap);

private Bitmap getPreview(URI uri) {
        File image = new File(uri);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        return null;

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    //opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(getApplicationContext(), 
                                              contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

I try to use ThumbnailUtil and etc but it didn't work. How to get ThumbnailImage on android 4.0? Thanks any reply.

like image 547
Steve Austin Avatar asked Oct 15 '13 06:10

Steve Austin


People also ask

Can we convert video to image?

This tool allows you to convert video to an image sequence. Upload any type of video and it will generate a list of JPG (JPEG) images. Upload your video, select size and frames per second, choose the part of the video you want to convert, and click "Convert to JPG!"


3 Answers

From API level 8 you can just do this:

String path = /*get video path*/;
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
                MediaStore.Images.Thumbnails.MINI_KIND);

Nice and simple :)

like image 52
Steffen Vangsgaard Avatar answered Oct 28 '22 14:10

Steffen Vangsgaard


If you want to extract a frame of a video you should use this class. The code should be something like that:

MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(path);
Bitmap extractedImage = media.getFrameAtTime(time, option);

Hope it´s useful

like image 40
Gonzalo Solera Avatar answered Oct 28 '22 13:10

Gonzalo Solera


Try This to get Thumbnail of a Video.

ImageView videoview;
 Bitmap thumb = ThumbnailUtils.createVideoThumbnail("YOUR VIDEO STRING PATH",   MediaStore.Images.Thumbnails.MINI_KIND);
 Matrix matrix = new Matrix();
 Bitmap bmThumbnail = Bitmap.createBitmap(thumb, 0, 0,
 thumb.getWidth(), thumb.getHeight(), matrix, true);
 videoview.setImageBitmap(bmThumbnail);

Edited: Use this method to get string path of Video URI.

/**
     * Try to return the absolute file path of the Gallery video.
     * 
     * @param context
     * @param uri
     * @return the file path or null
     */
    public static String getVideoPathFromGallary(final Context context,Uri contentUri) {
        String[] proj = {  MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION};
        Cursor cursor = ((Activity) context).managedQuery(contentUri, proj, null, null, null);
        if (cursor == null)
            return null;
        cursor.moveToFirst();
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
        long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
        System.out.println("size: " + fileSize);
        System.out.println("duration: " + duration);
        return cursor.getString(column_index);
    }
like image 36
Amit Gupta Avatar answered Oct 28 '22 12:10

Amit Gupta