A 正在开发 Android 项目。我需要提供 ContentProvider 以提供对某些目录的访问。FileProvider 这对我来说是一个很好的解决方案。是否可以使用 FileProvider 检索目录中的文件列表?
问问题
1708 次
1 回答
1
用户 ACTION_SEND_MULTIPLE 发送多个 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();
于 2015-07-22T06:31:58.380 回答