5

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.

Steve Austin
  • 103
  • 1
  • 1
  • 8

4 Answers4

20

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

8

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

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Gonzalo Solera
  • 1,182
  • 12
  • 26
1

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);
    }
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
  • 2
    ThumbnailUtils.createVideoThumbnail("YOUR VIDEO STRING PATH", MediaStore.Images.Thumbnails.MINI_KIND); return null so it did't work – Steve Austin Oct 15 '13 at 07:09
  • use getRealPathFromURI(Uri contentUri) this method to convert your Video Uri to string and use that in place of "YOUR VIDEO STRING PATH". – Amit Gupta Oct 15 '13 at 07:12
  • Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(getRealPathFromURI(Uri.parse("http://112.216.25.58:8888/VOD_LAUNCHER/media/youtube_sample3.mp4")), Thumbnails.MICRO_KIND); – Steve Austin Oct 15 '13 at 07:21
  • Cursor cursor = loader.loadInBackground(); cursor is null, it didn't work – Steve Austin Oct 15 '13 at 07:23
  • getVideoPathFromGallary Cursor cursor = ((Activity)context).managedQuery(contentUri, proj, null, null, null); cursor is null – Steve Austin Oct 15 '13 at 09:17
1

very simple !!! you can use glide library.

first add an imageView on videoView and use the code below:

>

 GlideApp.with(getApplicationContext()).load("empty")
    .thumbnail(GlideApp.with(getApplicationContext()).load("videoURL"))
    .into(imageView);

this code will download an image, and eventually, that leads to less internet consumption.