使用场景

调用手机自带的文件管理 选择文件

  1. /*
  2. * @Author: Park
  3. * @QQ:7323650
  4. * @Date: 2021-08-09
  5. * @content: 调用Android原生API选择文件并返回路径
  6. */
  7. "ui";
  8. ui.layout(
  9. <vertical>
  10. <button text="选择文件" id="选择" />
  11. </vertical>
  12. )
  13. ui.选择.on("click", function () {
  14. var fileType = "text/plain" //选择txt文件 文件类型参考:https://www.w3school.com.cn/media/media_mimeref.asp
  15. var intent = new Intent();
  16. intent.setType(fileType);
  17. intent.setAction(Intent.ACTION_GET_CONTENT);
  18. activity.startActivityForResult(intent, 5);
  19. })
  20. //监听选择文件回调
  21. activity.getEventEmitter().on("activity_result", (requestCode, resultCode, data) => {
  22. if (resultCode != -1) {
  23. toastLog("没有选择文件!");
  24. 文件路径 = ""
  25. return false;
  26. } else {
  27. var uri = data.getData();
  28. // log("uri: %s", uri.toString());
  29. let filepath = URIUtils_uriToFile(uri);
  30. 文件路径 = filepath
  31. toastLog(文件路径);
  32. }
  33. });
  34. //获取选择的文件路径 >> uri转文件路径
  35. function URIUtils_uriToFile (uri) {
  36. //Source : https://www.cnblogs.com/panhouye/archive/2017/04/23/6751710.html
  37. var r = null,
  38. cursor,
  39. column_index,
  40. selection = null,
  41. selectionArgs = null,
  42. isKitKat = android.os.Build.VERSION.SDK_INT >= 19,
  43. docs;
  44. if (uri.getScheme().equalsIgnoreCase("content")) {
  45. if (isKitKat && android.provider.DocumentsContract.isDocumentUri(activity, uri)) {
  46. if (String(uri.getAuthority()) == "com.android.externalstorage.documents") {
  47. docs = String(android.provider.DocumentsContract.getDocumentId(uri)).split(":");
  48. if (docs[0] == "primary") {
  49. return android.os.Environment.getExternalStorageDirectory() + "/" + docs[1];
  50. }
  51. } else if (String(uri.getAuthority()) == "com.android.providers.downloads.documents") {
  52. uri = android.content.ContentUris.withAppendedId(
  53. android.net.Uri.parse("content://downloads/public_downloads"),
  54. parseInt(android.provider.DocumentsContract.getDocumentId(uri))
  55. );
  56. } else if (String(uri.getAuthority()) == "com.android.providers.media.documents") {
  57. docs = String(android.provider.DocumentsContract.getDocumentId(uri)).split(":");
  58. if (docs[0] == "image") {
  59. uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  60. } else if (docs[0] == "video") {
  61. uri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  62. } else if (docs[0] == "audio") {
  63. uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  64. }
  65. selection = "_id=?";
  66. selectionArgs = [docs[1]];
  67. }
  68. }
  69. try {
  70. cursor = activity.getContentResolver().query(uri, ["_data"], selection, selectionArgs, null);
  71. if (cursor && cursor.moveToFirst()) {
  72. r = String(cursor.getString(cursor.getColumnIndexOrThrow("_data")));
  73. }
  74. } catch (e) {
  75. log(e);
  76. }
  77. if (cursor) cursor.close();
  78. return r;
  79. } else if (uri.getScheme().equalsIgnoreCase("file")) {
  80. return String(uri.getPath());
  81. }
  82. return null;
  83. }