浏览代码

Fix #27 Android implementation

Ben Hsieh 8 年前
父节点
当前提交
4d533a2af8
共有 1 个文件被更改,包括 18 次插入12 次删除
  1. 18
    12
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 18
- 12
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java 查看文件

@@ -63,15 +63,18 @@ public class RNFetchBlobFS {
63 63
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
64 64
             @Override
65 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 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 74
                     FileOutputStream fout = new FileOutputStream(f);
72 75
                     fout.write(stringToBytes(data, encoding));
73 76
                     fout.close();
74
-                    promise.resolve(null);
77
+                    promise.resolve(Arguments.createArray());
75 78
                 } catch (Exception e) {
76 79
                     promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
77 80
                 }
@@ -88,13 +91,16 @@ public class RNFetchBlobFS {
88 91
      * @param promise
89 92
      */
90 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 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 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 104
                     FileOutputStream os = new FileOutputStream(f);
99 105
                     byte [] bytes = new byte[data.size()];
100 106
                     for(int i=0;i<data.size();i++) {
@@ -119,7 +125,7 @@ public class RNFetchBlobFS {
119 125
      * @param promise
120 126
      */
121 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 129
             @Override
124 130
             protected Integer doInBackground(String... strings) {
125 131
                 try {
@@ -140,7 +146,7 @@ public class RNFetchBlobFS {
140 146
                             for(byte b : bytes) {
141 147
                                 asciiResult.pushInt((int)b);
142 148
                             }
143
-                            promise.resolve(bytes);
149
+                            promise.resolve(asciiResult);
144 150
                             break;
145 151
                         case "utf8" :
146 152
                             promise.resolve(new String(bytes));