0
我試圖從url加載視頻縮略圖到網格視圖中。一切正常,但我收到這條消息「/編舞:跳過了46幀!應用程序可能在其主線程上做了太多工作。」Android:應用程序可能在其主線程上做了太多工作
我知道它與主線程中的縮略圖加載有關,但我無法找到如何解決這個問題。
我還在使用一個設備來測試。
public class ThumbAdapter extends ArrayAdapter<Videos> {
Context context;
int ressource;
ThumbAdapter_Holder holder=new ThumbAdapter_Holder();
public ThumbAdapter(Context context, int resource, ArrayList<Videos> videoList) {
super(context, resource, videoList);
this.context=context;
this.ressource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if (view==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(ressource,parent,false);
holder.video_thum = (ImageView) view.findViewById(R.id.video_thum);
view.setTag(holder);
}
else {
holder=(ThumbAdapter_Holder) view.getTag();
}
try {
holder.video_thum.setImageBitmap(retriveVideoFrameFromVideo(getItem(position).getURL()));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return view;
}
class ThumbAdapter_Holder{
ImageView video_thum ;
}
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;
}
}
很顯然,對'mediaMetadataRetriever'的一次調用非常耗時,請考慮將此操作移至工作線程。 – Egor
您可以在https://developer.android.com/reference/android/os/AsyncTask.html中包裝整個retrieveVideFrameFromVideo查看如何實施AsyncTask – X3Btel