Android 如何使用FileProvider获取文件列表
A我在做Android项目。 我需要提供ContentProvider以提供对某些目录的访问。 FileProvider这对我来说是一个很好的解决方案。Android 如何使用FileProvider获取文件列表,android,android-contentprovider,android-fileprovider,Android,Android Contentprovider,Android Fileprovider,A我在做Android项目。 我需要提供ContentProvider以提供对某些目录的访问。 FileProvider这对我来说是一个很好的解决方案。 是否可以使用FileProvider检索目录中的文件列表?用户操作\u发送\u多个以发送多个URI // Set up an Intent to send back to apps that request files mResultIntent = new Intent("com.yourpacakgename.ACTION_SEND_MUL
是否可以使用FileProvider检索目录中的文件列表?用户操作\u发送\u多个以发送多个URI
// Set up an Intent to send back to apps that request files
mResultIntent = new Intent("com.yourpacakgename.ACTION_SEND_MULTIPLE");
// Get the files/res subdirectory;
File mResDir = new File(getFilesDir(), "res");
// Get the files in the res subdirectory
File[] mResFiles = mResDir.listFiles();
// Uri list
ArrayList<Uri> uriArrayList = new ArrayList<Uri>();
// Set the Activity's result to null to begin with
setResult(Activity.RESULT_CANCELED, null);
Uri fileUri = null;
for (int i = 0; i < mResFiles.length; i++) {
Log.i(TAG, mResFiles[i].getName());
// Use the FileProvider to get a content URI
try {
fileUri = FileProvider.getUriForFile(this, this.getPackageName() + ".fileprovider", mResFiles[i]);
// add current file uri to the list
uriArrayList.add(fileUri);
} catch (Exception e) {
Log.e(TAG, "The selected file can't be shared: " + mResFiles[i].getPath());
fileUri = null;
}
}
if (uriArrayList.size() != 0) {
// Put the UriList Intent
mResultIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriArrayList);
mResultIntent.setType("application/*");
// Grant temporary read permission to all apps
mResultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// if you want send this to a specific app
//grantUriPermission("pacakgename of client app", fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Set the result
this.setResult(Activity.RESULT_OK, mResultIntent);
} else {
// Set the result to failed
mResultIntent.setDataAndType(null, "");
this.setResult(RESULT_CANCELED, mResultIntent);
}
// Finish Activity and return Result to Caller
finish();
//设置发送回请求文件的应用程序的意图
mresultint=newintent(“com.yourpackgename.ACTION\u SEND\u MULTIPLE”);
//获取files/res子目录;
File mResDir=新文件(getFilesDir(),“res”);
//获取res子目录中的文件
文件[]mResFiles=mResDir.listFiles();
//Uri列表
ArrayList UrArrayList=新的ArrayList();
//首先将活动的结果设置为null
setResult(Activity.RESULT\u已取消,空);
urifileuri=null;
对于(int i=0;i
您尝试过什么?是的,我尝试过,但是只有一个示例如何获取一个具有特定名称的文件'file imagePath=new file(Context.getFilesDir(),“images”);File newFile=新文件(imagePath,“default_image.jpg”);Uri contentUri=getUriForFile(getContext(),“com.mydomain.fileprovider”,newFile);'我需要找到在目录中获取完整文件列表的方法