无法获取所选文件名的完整真实路径

ccgok5k5  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(816)

这个问题在这里已经有答案了

android kotlin:获取一个filenotfoundexception,其文件名是从文件选择器中选择的(3个答案)
android-获取从文件资源管理器中选择的.txt文件的实际路径(1个答案)
上个月关门了。
我没有获取当前路径var name“filepath”而是获取值e/file 路径:/document/29但我选择的文件存储在下载文件夹文件名是“test.xlsx”我需要原始路径文件名和文件扩展名传入fileinputstream()。我无法修复它…有人能给出代码吗
btnimport.setonclicklistener(新视图.onclicklistener(){

  1. @Override
  2. public void onClick(View v) {
  3. Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
  4. fileintent.setType("*/*");
  5. try {
  6. startActivityForResult(fileintent, requestcode);
  7. } catch (ActivityNotFoundException e) {
  8. lbl.setText("No activity can handle picking a file. Showing alternatives.");
  9. }
  10. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  11. if (data == null)
  12. return;
  13. switch (requestCode) {
  14. case requestcode:
  15. String FilePath = data.getData().getPath();
  16. Log.e("File path", FilePath);
  17. if (FilePath.contains("/root_path"))
  18. FilePath = FilePath.replace("/root_path", "");
  19. Log.e("New File path", FilePath);
  20. try {
  21. if (resultCode == RESULT_OK) {
  22. AssetManager am = this.getAssets();
  23. InputStream inStream;
  24. Workbook wb = null;
  25. try {
  26. inStream = new FileInputStream(FilePath);
  27. Log.e("Extension",FilePath.substring(FilePath.lastIndexOf(".")));
  28. if (FilePath.substring(FilePath.lastIndexOf(".")).equals(".xls")) {
  29. Log.e("File Type", "Selected file is XLS");
  30. wb = new HSSFWorkbook(inStream);
  31. }
  32. else if (FilePath.substring(FilePath.lastIndexOf(".")).equals(".xlsx")) {
  33. Log.e("File Type", "Selected file is XLSX");
  34. wb = new XSSFWorkbook(inStream);
  35. }
  36. else {
  37. wb = null;
  38. lbl.setText("Please select a valid Excel file");
  39. return;
  40. }
  41. inStream.close();
h7appiyu

h7appiyu1#

使用它将返回文件的实际路径

  1. public class FileUtils {
  2. public static String getRealPath(Context context, Uri uri) {
  3. String realPath;
  4. realPath = FileUtils.getRealPath_API19(context, uri);
  5. return realPath;
  6. }
  7. @SuppressLint("NewApi")
  8. public static String getRealPath_API19(final Context context, final Uri uri) {
  9. // DocumentProvider
  10. if (DocumentsContract.isDocumentUri(context, uri)) {
  11. // ExternalStorageProvider
  12. if (isExternalStorageDocument(uri)) {
  13. final String docId = DocumentsContract.getDocumentId(uri);
  14. final String[] split = docId.split(":");
  15. final String type = split[0];
  16. if ("primary".equalsIgnoreCase(type)) {
  17. return Environment.getExternalStorageDirectory() + "/"
  18. + split[1];
  19. }
  20. }
  21. // DownloadsProvider
  22. else if (isDownloadsDocument(uri)) {
  23. final String id = DocumentsContract.getDocumentId(uri);
  24. final Uri contentUri = ContentUris.withAppendedId(
  25. Uri.parse("content://downloads/public_downloads"),
  26. Long.parseLong(id));
  27. return getDataColumn(context, contentUri, null, null);
  28. }
  29. // MediaProvider
  30. else if (isMediaDocument(uri)) {
  31. final String docId = DocumentsContract.getDocumentId(uri);
  32. final String[] split = docId.split(":");
  33. final String type = split[0];
  34. Uri contentUri = null;
  35. if ("image".equals(type)) {
  36. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  37. } else if ("video".equals(type)) {
  38. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  39. } else if ("audio".equals(type)) {
  40. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  41. }
  42. final String selection = "_id=?";
  43. final String[] selectionArgs = new String[] { split[1] };
  44. return getDataColumn(context, contentUri, selection,
  45. selectionArgs);
  46. }
  47. }
  48. // MediaStore (and general)
  49. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  50. // Return the remote address
  51. if (isGooglePhotosUri(uri))
  52. return uri.getLastPathSegment();
  53. return getDataColumn(context, uri, null, null);
  54. }
  55. // File
  56. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  57. return uri.getPath();
  58. }
  59. return null;
  60. }
  61. /**
  62. * Get the value of the data column for this Uri. This is useful for
  63. * MediaStore Uris, and other file-based ContentProviders.
  64. *
  65. * @param context
  66. * The context.
  67. * @param uri
  68. * The Uri to query.
  69. * @param selection
  70. * (Optional) Filter used in the query.
  71. * @param selectionArgs
  72. * (Optional) Selection arguments used in the query.
  73. * @return The value of the _data column, which is typically a file path.
  74. */
  75. public static String getDataColumn(Context context, Uri uri,
  76. String selection, String[] selectionArgs) {
  77. Cursor cursor = null;
  78. final String column = "_data";
  79. final String[] projection = { column };
  80. try {
  81. cursor = context.getContentResolver().query(uri, projection,
  82. selection, selectionArgs, null);
  83. if (cursor != null && cursor.moveToFirst()) {
  84. final int index = cursor.getColumnIndexOrThrow(column);
  85. return cursor.getString(index);
  86. }
  87. } finally {
  88. if (cursor != null)
  89. cursor.close();
  90. }
  91. return null;
  92. }
  93. /**
  94. * @param uri
  95. * The Uri to check.
  96. * @return Whether the Uri authority is ExternalStorageProvider.
  97. */
  98. public static boolean isExternalStorageDocument(Uri uri) {
  99. return "com.android.externalstorage.documents".equals(uri
  100. .getAuthority());
  101. }
  102. /**
  103. * @param uri
  104. * The Uri to check.
  105. * @return Whether the Uri authority is DownloadsProvider.
  106. */
  107. public static boolean isDownloadsDocument(Uri uri) {
  108. return "com.android.providers.downloads.documents".equals(uri
  109. .getAuthority());
  110. }
  111. /**
  112. * @param uri
  113. * The Uri to check.
  114. * @return Whether the Uri authority is MediaProvider.
  115. */
  116. public static boolean isMediaDocument(Uri uri) {
  117. return "com.android.providers.media.documents".equals(uri
  118. .getAuthority());
  119. }
  120. /**
  121. * @param uri
  122. * The Uri to check.
  123. * @return Whether the Uri authority is Google Photos.
  124. */
  125. public static boolean isGooglePhotosUri(Uri uri) {
  126. return "com.google.android.apps.photos.content".equals(uri
  127. .getAuthority());
  128. }
  129. }

像这样使用

  1. String realPath = FileUtils.getRealPath(context, uri);

之后使用这个路径并创建新文件和文件的getrealname。

展开查看全部
fwzugrvs

fwzugrvs2#

我需要具有文件扩展名的文件名的原始路径来传入fileinputstream()。
不,您不需要“真正的路径”,因为您最好使用获得的uri来打开输入流。

  1. InputStream is = getContentResolver().openInputStream(data.getData());

像使用你想要的流一样使用它。

相关问题