gpt4 book ai didi

android - 如何使用 FileProvider 获取文件列表

转载 作者:行者123 更新时间:2023-12-03 20:20:52 24 4
gpt4 key购买 nike

A 正在开发 Android 项目。
我需要提供 ContentProvider 以提供对某些目录的访问。
FileProvider 这对我来说是一个很好的解决方案。
是否可以使用 FileProvider 检索目录中的文件列表?

最佳答案

用户 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();

关于android - 如何使用 FileProvider 获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com