Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. const platform = require("platform");
  2. const fs = require("file-system");
  3.  
  4. function getBase64String(file) {
  5. const data = file.readSync();
  6. if (platform.isIOS) {
  7. return data.base64EncodedStringWithOptions(0);
  8. }
  9. if (platform.isAndroid) {
  10. return android.util.Base64.encodeToString(data, android.util.Base64.NO_WRAP);
  11. }
  12. }
  13. openImagePicker () {
  14. console.log("OPENING IMAGE PICKER :)");
  15. var ref = this
  16. var context = imagepicker.create({ mode: "single" });
  17. context
  18. .authorize()
  19. .then(function() {
  20. debugger
  21. return context.present();
  22. })
  23. .then(function(selection) {
  24. debugger
  25. selection.forEach(function(selected) {
  26. // process the selected image
  27. debugger
  28. if (selected) {
  29. ref.getImageFilePath(selected).then(path => {
  30. console.log(`path: ${path}`);
  31. **********************************
  32. ref.uploadImage(path);
  33. **********************************
  34. debugger
  35. //this.uploadImage(path);
  36. });
  37. }
  38. });
  39. }).catch(function (e) {
  40. debugger
  41. // process error
  42. });
  43. },
  44. uploadImage(path) {
  45. let file = fs.File.fromPath(path);
  46. debugger
  47. console.log("************************************");
  48. const base64String = getBase64String(file);
  49. debugger
  50. this.files.push(base64String)
  51. debugger
  52. this.sendMessage()
  53. console.log("************************************");
  54. //const fileAsBlob = new Blob([file]);
  55. debugger
  56. },
  57. getImageFilePath(imageAsset) {
  58. return new Promise(resolve => {
  59. if (platform.isIOS) {
  60. const options = PHImageRequestOptions.new();
  61. options.synchronous = true;
  62. options.version =
  63. PHImageRequestOptionsVersion.Current;
  64. options.deliveryMode =
  65. PHImageRequestOptionsDeliveryMode.HighQualityFormat;
  66.  
  67. PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(
  68. imageAsset.ios,
  69. options,
  70. nsData => {
  71. // create file from image asset and return its path
  72. const tempFolderPath = fs.knownFolders
  73. .temp()
  74. .getFolder("nsimagepicker").path;
  75. const tempFilePath = fs.path.join(
  76. tempFolderPath,
  77. Date.now() + ".jpg"
  78. );
  79.  
  80. nsData.writeToFileAtomically(
  81. tempFilePath, true);
  82. resolve(tempFilePath);
  83. }
  84. );
  85. } else {
  86. // return imageAsset.android, since it 's the path of the file
  87. resolve(imageAsset.android);
  88. }
  89. });
  90. },
  91. }
  92. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement