问题描述
我的目标是在服务器上的不同文件夹中拥有多个mp4存储。 满足特定条件后,我想从文件夹中抓取其中一个视频。 如何从特定文件夹中提取随机视频文件? 这是我编写的代码:
public static Uri getVideoPath(String cond){
Uri tempPath;
if((cond.equals("01"))||(cond.equals("02"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/sunny/");
}else if((cond.equals("03"))||(cond.equals("04"))|| (cond.equals("05"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/some_sun/");
}else if((cond.equals("06"))||(cond.equals("07"))||(cond.equals("08"))||
(cond.equals("36"))||(cond.equals("37"))||(cond.equals("38"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/cloudy/");
}else if((cond.equals("09"))||(cond.equals("10"))||(cond.equals("27"))||(cond.equals("28"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/light_gray/");
}else if(cond.equals("11")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/fog/");
}else if((cond.equals("12"))||(cond.equals("13"))|| (cond.equals("14"))||
(cond.equals("39"))||(cond.equals("40"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/showers/");
}else if((cond.equals("15"))||(cond.equals("16"))|| (cond.equals("17"))||
(cond.equals("40"))||(cond.equals("41"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/thunderstorms/");
}else if(cond.equals("18")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/rain/");
}else if((cond.equals("19"))||(cond.equals("20"))|| (cond.equals("21"))||(cond.equals("43"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/flurries/");
}else if((cond.equals("22"))||(cond.equals("23"))||(cond.equals("44"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/snow/");
}else if((cond.equals("24"))||(cond.equals("25"))|| (cond.equals("26"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/ice/");
}else if(cond.equals("29")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/rain_and_snow/");
}else if(cond.equals("30")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/hot/");
}else if(cond.equals("31")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/cold/");
}else if(cond.equals("32")){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/windy/");
}else if((cond.equals("33"))||(cond.equals("34"))||(cond.equals("35"))){
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/clear/");
}else{
tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/clear/");
}
return tempPath;
}
1楼
首先,代替级联if-else,创建一个并put
条件和路径的每种组合。
然后,检索很简单:
public static Uri getVideoPath(String cond){
Uri tempPath = map.get(condition);
if (tempPath == null) tempPath = Uri.parse("http://www.personal.psu.edu/tmv105/video/clear/");
return tempPath;
}
您无法使用的listFiles()
获取目录列表,因为该列表仅适用于本地文件。
您必须目录列表。
假设您的服务器支持PHP,则最好添加一个index.php
,它只返回文本/纯文本文件列表。
一旦将其加载到数组中, 的其余代码便可以解决问题。
2楼
您可以执行以下操作:
File dir = new File(getVideoPath(condition));
Random gen = new Random();
File[] videos = dir.listFiles();
int random_index = gen.nextInt(videos.length);
return videos[random_index];