Java Code Examples for android.media.ThumbnailUtils#createVideoThumbnail()

The following examples show how to use android.media.ThumbnailUtils#createVideoThumbnail() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileTools.java    From AndroidDownload with Apache License 2.0 5 votes vote down vote up
public static Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
    Bitmap bitmap = null;
    // 获取视频的缩略图
    bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
    if(bitmap!= null){
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    }
    return bitmap;
}
 
Example 2
Source File: BaseImageDownloader.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.FROYO)
private InputStream getVideoThumbnailStream(String filePath) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
		Bitmap bitmap = ThumbnailUtils
				.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
		if (bitmap != null) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 0, bos);
			return new ByteArrayInputStream(bos.toByteArray());
		}
	}
	return null;
}
 
Example 3
Source File: WhatsappCameraActivity.java    From WhatsAppCamera with MIT License 5 votes vote down vote up
public void generateVideoThmb(String srcFilePath, File destFile) {
    try {
        Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(srcFilePath, 120);
        FileOutputStream out = new FileOutputStream(destFile);
        ThumbnailUtils.extractThumbnail(bitmap, 200, 200).compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 4
Source File: UploadVideoActivity.java    From patrol-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onContentLoaded(File file, String contentType) {
    targetFile = file;
    Bitmap thumb = ThumbnailUtils.createVideoThumbnail(targetFile.getAbsolutePath(),
            MediaStore.Video.Thumbnails.MINI_KIND);
    thumbUrl = FilesUtils.storeThumb(thumb);
    checkContentType(contentType);
    if (checkIsHaveMetaData(Uri.fromFile(file))){
        finishAddVideoToDB();
    }
}
 
Example 5
Source File: MyAdapter.java    From PixImagePicker with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    //Uri imageUri = Uri.fromFile(new File(list.get(position)));// For files on device
    File f = new File(list.get(position));
    Bitmap bitmap;
    if (f.getAbsolutePath().endsWith("mp4")) {
        ((Holder) holder).play.setVisibility(View.VISIBLE);
        bitmap = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
    } else {
        ((Holder) holder).play.setVisibility(View.GONE);
        bitmap = new BitmapDrawable(context.getResources(), f.getAbsolutePath()).getBitmap();
    }
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
    final float roundPx = (float) bitmap.getWidth() * 0.06f;
    roundedBitmapDrawable.setCornerRadius(roundPx);
    ((Holder) holder).iv.setImageDrawable(roundedBitmapDrawable);
    ((Holder) holder).iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(f.getAbsolutePath()));
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    intent.setDataAndType(Uri.parse(f.getAbsolutePath()), Files.probeContentType(f.toPath()));
                } else {
                    intent.setData(Uri.parse(f.getAbsolutePath()));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            context.startActivity(intent);
        }
    });
    /*Bitmap scaled = com.fxn.utility.Utility.getScaledBitmap(
        500f, com.fxn.utility.Utility.rotate(d,list.get(position).getOrientation()));*/

}
 
Example 6
Source File: CameraUtils.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
/** 创建视频缩略图,返回缩略图文件路径 */
public String createVideoThumbnail(String filePath, String fileName){
    Bitmap videoThumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);  //关键代码!!
    return saveBitmap(videoThumb, fileName);  //注:saveBitmap方法为保存图片并返回路径的private方法

}
 
Example 7
Source File: BitmapUtil.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
public static Bitmap createVideoThumbnail(String path) {
    return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);        
}
 
Example 8
Source File: ImageUtils.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
private static Bitmap getVideoFrame(String path) {
    return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}
 
Example 9
Source File: CaptureDemoFragment.java    From VideoCamera with Apache License 2.0 4 votes vote down vote up
private Bitmap getThumbnail() {
    if (filename == null) return null;
    return ThumbnailUtils.createVideoThumbnail(filename, Thumbnails.FULL_SCREEN_KIND);
}
 
Example 10
Source File: IconPreview.java    From FileManager with Apache License 2.0 4 votes vote down vote up
private static Bitmap getPreview(File file) {
    final boolean isImage = MimeTypes.isPicture(file);
    final boolean isVideo = MimeTypes.isVideo(file);
    final boolean isApk = file.getName().endsWith(".apk");

    Bitmap mBitmap = null;
    String path = file.getAbsolutePath();

    if (isImage) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, o);
        o.inJustDecodeBounds = false;

        if (o.outWidth != -1 && o.outHeight != -1) {
            final int originalSize = (o.outHeight > o.outWidth) ? o.outWidth
                    : o.outHeight;
            o.inSampleSize = originalSize / mWidth;
        }
 
        mBitmap = BitmapFactory.decodeFile(path, o);

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    } else if (isVideo) {
        mBitmap = ThumbnailUtils.createVideoThumbnail(path,
                MediaStore.Video.Thumbnails.MICRO_KIND);

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    } else if (isApk) {
        //Log.w("ruijie", path);
        final PackageInfo packageInfo = pm.getPackageArchiveInfo(path,
                PackageManager.GET_ACTIVITIES);

        if (packageInfo != null) {
            final ApplicationInfo appInfo = packageInfo.applicationInfo;

            if (appInfo != null) {
                appInfo.sourceDir = path;
                appInfo.publicSourceDir = path;
                final Drawable icon = appInfo.loadIcon(pm);

                if (icon != null) {
                    mBitmap = ((BitmapDrawable) icon).getBitmap();
                }
            }
        } else {
            // load apk icon from /res/drawable/..
            mBitmap = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.type_apk);
        }

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    }
    return null;
}
 
Example 11
Source File: Thumbnail.java    From UMS-Interface with GNU General Public License v3.0 4 votes vote down vote up
/**
 * get the thumbnail for image video and so on
 * <b>note that it will take much mTime and block</b>
 * */
public static Bitmap getThumbnail(String path,int size)
{
    String extensionString = FileTool.getExtension(path);
    Bitmap thumbnailBitmap = null;
    if(isStringIn(extensionString,videoExtension))
    {
        thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(path,size);
    }
    if(isStringIn(extensionString,imageExtension))
    {
        int width=0,height=0;
        switch (size)
        {
            case MICRO_KIND:
                width = 96;
                height = 96;
                break;
            case MINI_KIND:
                width = 512;
                height = 384;
                break;
            case FULL_SCREEN_KIND:
                width = 700;
                height = 1200;
                break;
        }
        thumbnailBitmap = getImageThumbnail(path,width,height);
    }
    if(extensionString.equalsIgnoreCase("apk"))
    {
        if(mContext!=null) {
            Drawable drawable = getApkIcon(mContext, path);
            if(drawable!=null)
            {
                thumbnailBitmap = drawableToBitamp(drawable);
            }
        }
    }
    return thumbnailBitmap;
}
 
Example 12
Source File: VideoListAdapter.java    From PLDroidShortVideo with Apache License 2.0 4 votes vote down vote up
private Bitmap getVideoThumbnail(String videoPath) {
    return ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Video.Thumbnails.MINI_KIND);
}
 
Example 13
Source File: HelperMimeType.java    From iGap-Android with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap doInBackground(Object... params) {

    return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
}
 
Example 14
Source File: ImageUtils.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
private static Bitmap getVideoFrame(String path) {
    return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
}
 
Example 15
Source File: Thumbnail.java    From UMS-Interface with GNU General Public License v3.0 4 votes vote down vote up
/**
 * get the thumbnail for image video and so on
 * <b>note that it will take much mTime and block</b>
 * */
public static Bitmap getThumbnail(String path,int size)
{
    String extensionString = FileTool.getExtension(path);
    Bitmap thumbnailBitmap = null;
    if(isStringIn(extensionString,videoExtension))
    {
        thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(path,size);
    }
    if(isStringIn(extensionString,imageExtension))
    {
        int width=0,height=0;
        switch (size)
        {
            case MICRO_KIND:
                width = 96;
                height = 96;
                break;
            case MINI_KIND:
                width = 512;
                height = 384;
                break;
            case FULL_SCREEN_KIND:
                width = 700;
                height = 1200;
                break;
        }
        thumbnailBitmap = getImageThumbnail(path,width,height);
    }
    if(extensionString.equalsIgnoreCase("apk"))
    {
        if(mContext!=null) {
            Drawable drawable = getApkIcon(mContext, path);
            if(drawable!=null)
            {
                thumbnailBitmap = drawableToBitamp(drawable);
            }
        }
    }
    return thumbnailBitmap;
}
 
Example 16
Source File: FermiPlayerUtils.java    From FimiX8-RE with MIT License 4 votes vote down vote up
public static Bitmap createVideoThumbnail(String filePath) {
    return ThumbnailUtils.createVideoThumbnail(filePath.replace("file://", ""), 1);
}
 
Example 17
Source File: TakeVideoActivity.java    From MultiMediaSample with Apache License 2.0 4 votes vote down vote up
public static Bitmap getVideoThumbnail(String filePath) {
    return ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MINI_KIND);
}
 
Example 18
Source File: VideoSelectPreference.java    From VIA-AI with MIT License 4 votes vote down vote up
public void setVideoPath(String path) {
    boolean isValid = false;
    boolean isFileExist = false;
    mVideoPathValue = path;

    do {
        if(path == null || path == "") break;

        isFileExist = isFileExist(path);
        if(isFileExist) {
            try {
                Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);

                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(path);
                mWidth = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
                mHeight = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
                this.setSummary("    Video Path : " + path + " < " + mWidth + " x " + mHeight + " >");
                persistString((String) path);
                if (thumb != null) {
                    setPreviewBitmap(thumb);
                } else {
                    setPreviewResource(mContext.getResources().getDrawable(R.drawable.icon_unknown_video_fmt));
                }
                isValid = true;
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    } while(false);

    if(!isValid) {
        if(!isFileExist) {
            this.setSummary("    File not exist or permission denied. Click to select a new \".mp4 \" file...");
        }
        else {
            this.setSummary("    Fail to parse bitmap in this file. Click to select a new \".mp4 \" file...");
        }
        persistString("");
        if(isEnabled()) {
            this.setBackgroundColor(Color.rgb(170, 30, 30));
        }
        else {
            this.setBackgroundColor(Color.TRANSPARENT);
        }
    }
    else {
        this.setBackgroundColor(Color.TRANSPARENT);
    }

    if(mView != null) {
        super.onBindView(mView);    // use base class's oBindView to avoid recursive.
    }
}
 
Example 19
Source File: HxChatPresenter.java    From FamilyChat with Apache License 2.0 3 votes vote down vote up
/**
 * 获取视频的缩略图
 * 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
 * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
 *
 * @param videoPath 视频的路径
 * @param width     指定输出视频缩略图的宽度
 * @param height    指定输出视频缩略图的高度度
 * @param kind      参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
 *                  其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
 * @return 指定大小的视频缩略图
 */
private Bitmap createVideoThumbBitmap(String videoPath, int width, int height, int kind)
{
    Bitmap bitmap = null;
    // 获取视频的缩略图
    bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    return bitmap;
}
 
Example 20
Source File: MediaStoreUtils.java    From BigApp_WordPress_Android with Apache License 2.0 3 votes vote down vote up
/**
 * 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
 * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
 * 
 * @param videoPath
 *            视频的路径
 * @param width
 *            指定输出视频缩略图的宽度
 * @param height
 *            指定输出视频缩略图的高度度
 * @param kind
 *            参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
 *            其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
 * @return 指定大小的视频缩略图
 */
public static Bitmap getVideoThumbnail(String videoPath, int width,
		int height, int kind) {
	Bitmap bitmap = null;
	bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
	if (width > 0 && height > 0) {
		bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
				ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
	}
	return bitmap;
}