在我的Android应用程序中,我使用下面的代码列出用于选择和上传的mp3文件.
我想知道是否可以过滤文件的持续时间及其类型.
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
startActivityForResult(Intent.createChooser(intent, "Select your Audio"), Global.FILE_FROM_SD);
}
catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(WizardStep1.this,
"Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
我不这么认为,或者至少不是你开始的方式。
我相信一旦您创建了意图选择器并打开了另一个文件管理器,那么您就无法控制向您显示该特定文件管理器的数据,例如按特定条件过滤。
我在这里看到的解决方案是创建一个方法,将sdcard 中的所有音频文件返回到一个数组中,然后使用过滤器来确定文件的扩展名 (.mp3),并结合获取文件持续时间的方法。
将过滤后的内容添加到新数组中,然后对过滤后的文件执行您想要的操作,例如显示在 a 中ListView并让用户选择他想要上传的文件。
伪代码如下所示:
ArrayList<String> filteredFiles = new ArrayList<String>();
String[] audioFiles = getAllAudioFiles();
for (String file : audioFiles) {
if (file.endsWith(".mp3") && getDuration(file) >= DURATION_CONSTANT) {
filteredFiles.add(file);
}
}
Run Code Online (Sandbox Code Playgroud)
filteredFiles然后显示a 中的内容ListView并提供用于选择/上传文件的适当逻辑。
| 归档时间: |
|
| 查看次数: |
440 次 |
| 最近记录: |