从文件获取媒体标题
我使用 import java.io.File;
从我的 SD 卡导入所有音乐文件, 现在我想要使用 Mediastore 的文件标题,但我该怎么做? 这是我现在使用的代码。
public class ListFiles extends ListActivity {
private List<String> directoryEntries = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
File directory = new File(i.getStringExtra("directory"));
if (directory.isDirectory()){
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2) {
return -Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});
this.directoryEntries.clear();
for (File file : files) {
this.directoryEntries.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,R.layout.file_row, this.directoryEntries);
this.setListAdapter(directoryList);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File clickedFile = new File(this.directoryEntries.get(position));
Intent i = getIntent();
i.putExtra("clickedFile", clickedFile.toString());
setResult(RESULT_OK, i);
finish();
}
}
谢谢。
I used import java.io.File;
to import all music files from my sdcard,
Now i want the Title's of the files using Mediastore but how do I do that?
This is the code I Use now.
public class ListFiles extends ListActivity {
private List<String> directoryEntries = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
File directory = new File(i.getStringExtra("directory"));
if (directory.isDirectory()){
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2) {
return -Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});
this.directoryEntries.clear();
for (File file : files) {
this.directoryEntries.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,R.layout.file_row, this.directoryEntries);
this.setListAdapter(directoryList);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File clickedFile = new File(this.directoryEntries.get(position));
Intent i = getIntent();
i.putExtra("clickedFile", clickedFile.toString());
setResult(RESULT_OK, i);
finish();
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经使用 MediaStore:类概述 - 媒体提供程序包含内部和外部存储设备上所有可用媒体的元数据。
您可以参考这两个例子来了解MediaStore的使用
列出视频 和 列表音频
I have done that for Video files using MediaStore : Class Overview - The Media provider contains meta data for all available media on both internal and external storage devices.
You can refer these two example to understand the use of MediaStore
List Video and List Audio