如何在 Xamarin Android 中获取具有特定扩展名的文件列表? [英] How to get the list of files with specific extension in Xamarin Android?
本文介绍了如何在 Xamarin Android 中获取具有特定扩展名的文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要关于从 android 设备的外部存储中获取 PDF 文件列表的建议
I need suggestion on getting a list of PDF files from the external storage in android device
推荐答案
1.可以遍历文件夹,过滤PDF文件:
1.You could traverse the folder and filter the PDF files:
public void Search_Pdf_Dir(File dir)
{
string pdfPattern = ".pdf";
File[] FileList = dir.ListFiles();
if (FileList != null)
{
for (int i = 0; i < FileList.Length; i++)
{
if (FileList[i].IsDirectory)
{
Search_Pdf_Dir(FileList[i]);
}
else
{
if (FileList[i].Name.EndsWith(pdfPattern))
{
//here you have that file.
}
}
}
}
}
然后你可以调用 Search_Pdf_Dir(Android.OS.Environment.ExternalStorageDirectory);
2.使用 MediaStore - Uri 查询所有类型的文件:
2.use MediaStore - Uri to query all types of files :
ContentResolver cr = ContentResolver;
Android.Net.Uri uri = MediaStore.Files.GetContentUri("external");
// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
string[] projection = null;
string selectionMimeType = MediaStore.Files.FileColumns.MediaType + "=?";
string mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension("pdf");
string[] selectionArgsPdf = new string[] { mimeType };
string sortOrder = null;
var allPdfFiles = cr.Query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
while (allPdfFiles.MoveToNext())
{
int column_index = allPdfFiles.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.Data);
string filePath = allPdfFiles.GetString(column_index);//the pdf path
}
这篇关于如何在 Xamarin Android 中获取具有特定扩展名的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文