英文:
How to show only videos folder?
问题
我正在开发一个安卓应用程序,请告诉我,如何只显示视频文件夹?
这段代码会显示设备中的所有文件夹,但我想只显示视频文件夹。应该如何做呢?
这是我的代码:
public class MainActivity extends ListActivity {
private List<String> listfile = new ArrayList<String>();
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
File fileroot = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File[] files = fileroot.listFiles();
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = fileroot.listFiles(fileFilter);
System.out.println(files.length);
if (files.length == 0) {
//
} else {
for (int i = 0; i < files.length; i++) {
listfile.add(files[i].getName());
}
ArrayAdapter<String> dis = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listfile);
setListAdapter(dis);
}
}
}
英文:
I am working on an android app, please tell me, How to show only videos folder?
this code shows all folder in my device, But I want to show only videos folder. How to do that?
here is my code
public class MainActivity extends ListActivity {
private List<String> listfile = new ArrayList<String>();
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
File fileroot = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File[] files = fileroot.listFiles();
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = fileroot.listFiles(fileFilter);
System.out.println(files.length);
if (files.length == 0) {
//
} else {
for (int i = 0; i < files.length; i++) {
listfile.add(files[i].getName());
}
ArrayAdapter <String> dis = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listfile);
setListAdapter(dis);
}
}
}
答案1
得分: 1
在Android中,没有简单的方法来完成这个任务。假设在特定设备上只会有一个名为videos/
的目录是错误的。即使存在这样的目录,也不能假设视频文件只会存储在那里,而不会出现在其他地方。
如果你真的只想扫描一个目录,那么一个简单的方法是在你的应用程序第一次启动时提示用户选择位置,并将所选位置存储起来以备将来使用。实际上,许多处理媒体的Android应用程序都采取了这种方法,尽管它们可能允许用户选择多个位置。
如果你想要列出_视频文件_,而不仅仅是特定目录的内容,那么可以通过查询Android媒体存储来实现。这个存储在文件被传输到设备时会被更新(或者应该被更新)。每个文件都会被打上各种属性的标签,这些属性是根据Android内置的各种检查方法进行设置的。现代Android版本在识别视频文件方面表现得相当不错,尽管过去不总是如此。
要使用媒体存储,你需要在特定的Context
上的ContentResolver
上执行查询,检查一个标志,该标志表示内容是视频。然后,你需要迭代结果,提取所需的各种信息。
老实说,我还没有在Android中使用过媒体存储来处理视频,但我用过来处理音频。我认为基本原理应该是相同的。如果有帮助的话,我在这里有一些关于音频查询的示例代码:
英文:
There's no simple way to do this in Android. It's a mistake to assume that there will be only one directory videos/
on a particular device. Even if there is, you can't assume that video files will be stored there, and nowhere else.
If you really only want to scan one directory, then a simple approach is to prompt the user for the location when your app first starts, and store the selected location for future use. Many android apps that handle media do, in fact, take this approach -- although they might allow the user to select multiple locations.
If you want to list video files, rather than the contents of a specific directory, then the way to do this is to query the Android media store. This store is (or should be) updated whenever files are transferred to the device. Each file is tagged with various attributes according to various inspection methods built into Android. Modern Android versions are pretty good at recognizing video files, although that wasn't always the case.
To use the media store, you'd do a query on the ContentResolver
for a specific Context
, testing for a flag that indicates that the content is video. Then you'd iterate the results, extracting the various pieces of information you need.
To be honest, I've not used the Android media store for video, although I have for audio. I think the basic principles will be the same. I have some sample code for audio queries here, if that's any help:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论