First You need to create VideoRequestHandler
public class VideoRequestHandler extends RequestHandler{
public String SCHEME_VIDEO="video";
@Override
public boolean canHandleRequest(Request data)
{
String scheme = data.uri.getScheme();
return (SCHEME_VIDEO.equals(scheme));
}
@Override
public Result load(Request data, int arg1) throws IOException
{
Bitmap bm;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
Size size = new Size(250, 250);
bm = ThumbnailUtils.createVideoThumbnail(new File(data.uri.getPath()), size, null);
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
bm = ThumbnailUtils.createVideoThumbnail(data.uri.getPath(), MediaStore.Images.Thumbnails.MINI_KIND);
}
return new Result(bm,LoadedFrom.DISK);
}
}
After That
VideoRequestHandler videoRequestHandler;
Picasso picassoInstance;
Build only once
videoRequestHandler = new VideoRequestHandler();
picassoInstance = new Picasso.Builder(context.getApplicationContext())
.addRequestHandler(videoRequestHandler)
.build();
Then load file from path
picassoInstance.load(VideoRequestHandler.SCHEME_VIDEO+":"+filepath).into(holder.videoThumbnailView);
Updated October 2020
ThumbnailUtils.createVideoThumbnail(data.uri.getPath(), MediaStore.Images.Thumbnails.MINI_KIND);
is deprecated in Android Q. I will write in Kotlin:
val bm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ThumbnailUtils.createVideoThumbnail(
File(data.uri.path!!),
Size(200f.toPx(), 200f.toPx()),
CancellationSignal()
)
} else {
ThumbnailUtils.createVideoThumbnail(
data.uri.path!!,
MediaStore.Images.Thumbnails.MINI_KIND
)
}
to Px is an extension function which is like below;
fun Float.toPx() =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics)
.toInt()
You can use any dp value for it :) I hope this can help you :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…