1

我正在尝试在目录中加载视频的视频缩略图和文件名文本。由于目录下可能有很多视频,所以我需要使用AsyncTask。当我将代码放在 AsyncTask 中的 getView() 中时,我得到了一堆错误。请告诉我我做错了什么。

public class MainActivity extends ListActivity {

String dirNameSlash = "/videos/";
String path = Environment.getExternalStorageDirectory() + dirNameSlash;
String root_sd = Environment.getExternalStorageDirectory().toString();
String theFile = root_sd + "/videos/";

File dir = new File(path);
{
    assert (dir.exists());
    assert (dir.isDirectory());

}

String[] videoFileList = dir.list();

public class MyThumbnaildapter extends ArrayAdapter<String> {

    public MyThumbnaildapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.activity_main, parent, false);
        }

        TextView textfilePath = (TextView) row.findViewById(R.id.FilePath);
        textfilePath.setText(videoFileList[position]);
        ImageView imageThumbnail = (ImageView) row
                .findViewById(R.id.Thumbnail);

        Bitmap bmThumbnail;
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(path
                + videoFileList[position], Thumbnails.MICRO_KIND);
        imageThumbnail.setImageBitmap(bmThumbnail);

        return row;
    }

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new MyThumbnaildapter(MainActivity.this,
            R.layout.activity_main, videoFileList));

    ListView lv = getListView();

}
4

1 回答 1

0

我建议你在 AsyncTask 的地方使用 Volley。与 AsyncTask 相比,它很容易实现并且做得更好。

看看这里的实现http://java.dzone.com/articles/android-%E2%80%93-volley-library

这是 Ficus Kirkpatrick,他在 Google I/O 2013 上解释 Volley http://www.youtube.com/watch?v=yhv8l9F44qo

Volley 最好的 Live 示例是 Google Play 商店。

于 2013-10-22T18:16:32.210 回答