有没有人可以分享一下Android 11内部和外部存储的文件拾取器的完整解决方案
发布于 2021-09-20 06:36:38
您可以使用SAF(存储访问框架)挑选文件
适用于单文件的
// Request code for selecting a PDF document.
const val PICK_PDF_FILE = 2
fun openFile(pickerInitialUri: Uri) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/pdf"您还可以使用SAF打开目录
fun openDirectory(pickerInitialUri: Uri) {
// Choose a directory using the system's file picker.
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when it loads.
putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
}
startActivityForResult(intent, your-request-code)
}
// Optionally, specify a URI for the file that should appear in the
// system file picker when it loads.
putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
}
startActivityForResult(intent, PICK_PDF_FILE)
}获取文件的URI,像这样的
override fun onActivityResult(
requestCode: Int, resultCode: Int, resultData: Intent?) {
if (requestCode == your-request-code
&& resultCode == Activity.RESULT_OK) {
// The result data contains a URI for the document or directory that
// the user selected.
resultData?.data?.also { uri ->
// Perform operations on the document using its URI.
}
}
}发布于 2021-09-20 06:43:59
**For Single File**
// Request code for selecting a PDF document.
const val PICK_PDF_FILE = 2
fun openFile(pickerInitialUri: Uri) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/pdf"发布于 2022-02-07 11:27:33
这里的代码为安卓11文件上传使用意图,而且你也不必提及AndroidManifest.xml文件中的MANAGE_EXTERNAL_STORAGE权限,我已经实现了没有这个权限。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("application/pdf");
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(chooseFile, Constants.REQUEST_PICK_FILE);
}else{
String[] types = new String[]{"pdf"};
FilePickerBuilder.getInstance().setMaxCount(UPLOAD_LIMIT - attachmentModelList.size())
.setActivityTheme(R.style.AppTheme)
.setActivityTitle(mActivity.getString(R.string.select_documents))
/*.sortDocumentsBy(SortingTypes.NAME)*/
.enableDocSupport(false)
.addFileSupport("PDF", types)
/*.setSelectedFiles(masterList !!)*/
.pickFile(this);
}以下是onActivityResult代码选择文件名和其他信息
if (requestCode == Constants.REQUEST_PICK_FILE &&
resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String mimeType = getActivity().getContentResolver().getType(uri);
String filename;
if (mimeType == null) {
String path = CommonMethod.getPath(getActivity(), uri);
// if (path == null) {
// filename = FilenameUtils.getName(uri.toString());
// } else {
File file = new File(path);
filename = file.getName();
// }
} else {
Uri returnUri = data.getData();
Cursor returnCursor = getActivity().getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
filename = returnCursor.getString(nameIndex);
String size = Long.toString(returnCursor.getLong(sizeIndex));
}
File fileSave = getActivity().getExternalFilesDir(null);
String sourcePath = getActivity().getExternalFilesDir(null).toString();
File targetFile = null;
try {
targetFile = new File(sourcePath + "/" + filename);
boolean success = CommonMethod.copyFileStream(new File(sourcePath + "/" + filename), uri, getActivity());
} catch (Exception e) {
e.printStackTrace();
}
if (targetFile != null) {
String fileName = Utils.getFileName(getActivity(),Uri.fromFile(targetFile));
String filePath = targetFile.getPath();
}https://stackoverflow.com/questions/69249858
复制相似问题