我尝试以列表格式从sdcard中获取所有.mp4视频

时间:2016-01-21 17:32:48

标签: android video android-studio

我尝试以列表格式从sdcard中获取所有.mp4视频。 但是有些事情失败了,这对我没用。 当你启动应用程序时,我什么都没得到,都是白色的。 什么可以?以及如何解决? 基数来自here

感谢

public class VideoScan extends ListActivity{

File dir = new File("/sdcard/");
String[] videoFileList  = dir.list(
        new FilenameFilter()
{
    public boolean accept(File dir, String videoFileList)
    {
        return videoFileList.endsWith(".mp4");

    }
});


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_video_scan, 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(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(VideoScan.this, R.layout.activity_video_scan, videoFileList));
}

2 个答案:

答案 0 :(得分:1)

也许路径&#34; / sdcard /&#34;是错的。

因为现在sdcard的路径是&#34; / emulator0 /....&#34;,你可以使用

File file = Environment.getExternalStorageDirectory();

获取文件。

你可以尝试这种方法。

答案 1 :(得分:0)

试试这个:

    public class MainActivity extends ListActivity {

/* Get the absolute path */
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File dirs = new File(path);

/**
 * array videoFileList saves the mp4 file name,
 * so in the adapter ,You need to add the path Manually
 */
String[] videoFileList  = dirs.list(
        new FilenameFilter()
{
    public boolean accept(File dir, String videoFileList)
    {
        return videoFileList.endsWith(".mp4");

    }
});

//String[] videoFileList  = dirs.list();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.e("ssssss",videoFileList[0]);

    setListAdapter(new MyThumbnaildapter(MainActivity.this, R.layout.activity_video_scan, videoFileList));
}


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_video_scan, parent, false);
        }

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

        Bitmap bmThumbnail;

        // path+"/"+videoFileList[position] 
        // add the path mannually
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(path+"/"+videoFileList[position], Thumbnails.MICRO_KIND);
        imageThumbnail.setImageBitmap(bmThumbnail);

        return row;
    }

}
}