本文介绍了我想从Android中服务器的视频URL创建缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码,
public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
else
mediaMetadataRetriever.setDataSource(videoPath);
// mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
} catch (Exception e) {
e.printStackTrace();
throw new Throwable(
"Exception in retriveVideoFrameFromVideo(String videoPath)"
+ e.getMessage());
} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}
这是创建缩略图",但是我花了很多时间在ListView
和ListView
挂断时使用它.
This is Create thumbnail but take much time I used this with ListView
then ListView
being hangup.
推荐答案
如果您使用的是RecycleView
,则需要在onBindViewHolder()
中的异步方法中运行此任务;如果您使用的是中>:
You need run this task in Async Method Like this in onBindViewHolder()
if you are using RecycleView
or put on getView()
if your are using ListView
:
new AsyncTask<String, String, String>() {
Bitmap bitmapVideo;
@Override
protected String doInBackground(String... strings) {
try {
//Your method call here
bitmapVideo =retriveVideoFrameFromVideo(strings[0]);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String id) {
super.onPostExecute(id);
if (bitmapVideo != null) {
//Load your bitmap here
holder.imgVideoThumb.setImageBitmap(bitmapVideo);
}
}
}.execute(getYourVideolink());
为了获得更高的效率,请将位图图像保存在本地,然后在调用AsyncTask()
之前检查天气,如果该图像已从本地加载并且没有新内容再次运行AsyncTask()
,则该图像已经保存在本地了
For better efficiency you save the bitmap image in local and before calling AsyncTask()
check weather this image is already save in local if its their than load from local and no new to run AsyncTask()
again
这篇关于我想从Android中服务器的视频URL创建缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!