Browse Source

Fix #27 Android implementation

Ben Hsieh 8 years ago
parent
commit
4d533a2af8
1 changed files with 18 additions and 12 deletions
  1. 18
    12
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 18
- 12
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

63
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
63
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
64
             @Override
64
             @Override
65
             protected Integer doInBackground(String... args) {
65
             protected Integer doInBackground(String... args) {
66
-                String path = args[0];
67
-                String encoding = args[1];
68
-                String data = args[2];
69
-                File f = new File(path);
70
                 try {
66
                 try {
67
+                    String path = args[0];
68
+                    String encoding = args[1];
69
+                    String data = args[2];
70
+                    File f = new File(path);
71
+                    File dir = f.getParentFile();
72
+                    if(!dir.exists())
73
+                        dir.mkdirs();
71
                     FileOutputStream fout = new FileOutputStream(f);
74
                     FileOutputStream fout = new FileOutputStream(f);
72
                     fout.write(stringToBytes(data, encoding));
75
                     fout.write(stringToBytes(data, encoding));
73
                     fout.close();
76
                     fout.close();
74
-                    promise.resolve(null);
77
+                    promise.resolve(Arguments.createArray());
75
                 } catch (Exception e) {
78
                 } catch (Exception e) {
76
                     promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
79
                     promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
77
                 }
80
                 }
88
      * @param promise
91
      * @param promise
89
      */
92
      */
90
     static public void writeFile(String path, ReadableArray data, final Promise promise) {
93
     static public void writeFile(String path, ReadableArray data, final Promise promise) {
91
-        AsyncTask<Object, Integer, Integer> task = new AsyncTask<Object, Integer, Integer>() {
94
+        AsyncTask<Object, Void, Void> task = new AsyncTask<Object, Void, Void>() {
92
             @Override
95
             @Override
93
-            protected Integer doInBackground(Object... args) {
94
-                String path = String.valueOf(args[0]);
95
-                ReadableArray data = (ReadableArray) args[2];
96
-                File f = new File(path);
96
+            protected Void doInBackground(Object... args) {
97
                 try {
97
                 try {
98
+                    String path = (String)args[0];
99
+                    ReadableArray data = (ReadableArray) args[1];
100
+                    File f = new File(path);
101
+                    File dir = f.getParentFile();
102
+                    if(!dir.exists())
103
+                        dir.mkdirs();
98
                     FileOutputStream os = new FileOutputStream(f);
104
                     FileOutputStream os = new FileOutputStream(f);
99
                     byte [] bytes = new byte[data.size()];
105
                     byte [] bytes = new byte[data.size()];
100
                     for(int i=0;i<data.size();i++) {
106
                     for(int i=0;i<data.size();i++) {
119
      * @param promise
125
      * @param promise
120
      */
126
      */
121
     static public void readFile(String path, String encoding, final Promise promise ) {
127
     static public void readFile(String path, String encoding, final Promise promise ) {
122
-        AsyncTask task = new AsyncTask<String, Integer, Integer>() {
128
+        AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
123
             @Override
129
             @Override
124
             protected Integer doInBackground(String... strings) {
130
             protected Integer doInBackground(String... strings) {
125
                 try {
131
                 try {
140
                             for(byte b : bytes) {
146
                             for(byte b : bytes) {
141
                                 asciiResult.pushInt((int)b);
147
                                 asciiResult.pushInt((int)b);
142
                             }
148
                             }
143
-                            promise.resolve(bytes);
149
+                            promise.resolve(asciiResult);
144
                             break;
150
                             break;
145
                         case "utf8" :
151
                         case "utf8" :
146
                             promise.resolve(new String(bytes));
152
                             promise.resolve(new String(bytes));