Browse Source

#2 refactor android code

Ben Hsieh 8 years ago
parent
commit
d077c7e682

+ 1
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

37
     @ReactMethod
37
     @ReactMethod
38
     public void flush(String taskId) {
38
     public void flush(String taskId) {
39
         try {
39
         try {
40
-            new File(RNFetchBlobFS.TempFilePath + taskId).delete();
40
+            new File(RNFetchBlobFS.getTmpPath(this.getReactApplicationContext(), taskId)).delete();
41
         } catch(Exception err) {
41
         } catch(Exception err) {
42
             WritableMap args = Arguments.createMap();
42
             WritableMap args = Arguments.createMap();
43
             args.putString("event", "error");
43
             args.putString("event", "error");

+ 5
- 4
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

17
  */
17
  */
18
 public class RNFetchBlobFS {
18
 public class RNFetchBlobFS {
19
 
19
 
20
-    static String DocumentDir = Environment.getExternalStorageDirectory().toString();
21
-    static String TempFilePath = Environment.getExternalStorageDirectory().toString() + "/RNFetchBlobCache/dltmp";
22
-
23
     ReactApplicationContext mCtx;
20
     ReactApplicationContext mCtx;
24
     DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
21
     DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
25
 
22
 
23
+    static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
24
+        return ctx.getFilesDir() + "/RNFetchBlobTmp_" + taskId;
25
+    }
26
+
26
 
27
 
27
     RNFetchBlobFS(ReactApplicationContext ctx) {
28
     RNFetchBlobFS(ReactApplicationContext ctx) {
28
         this.mCtx = ctx;
29
         this.mCtx = ctx;
32
     public void readStream(String taskId, String encoding) {
33
     public void readStream(String taskId, String encoding) {
33
         try {
34
         try {
34
 
35
 
35
-            FileInputStream fs = mCtx.openFileInput(RNFetchBlobFS.TempFilePath + taskId);
36
+            FileInputStream fs = mCtx.openFileInput(mCtx.getFilesDir() + "/fetchblobtmp_"+ taskId);
36
             byte[] buffer = new byte[1024];
37
             byte[] buffer = new byte[1024];
37
             int cursor = 0;
38
             int cursor = 0;
38
             boolean error = false;
39
             boolean error = false;

+ 4
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFileHandler.java View File

2
 
2
 
3
 import com.facebook.react.bridge.Arguments;
3
 import com.facebook.react.bridge.Arguments;
4
 import com.facebook.react.bridge.Callback;
4
 import com.facebook.react.bridge.Callback;
5
+import com.facebook.react.bridge.ReactApplicationContext;
5
 import com.facebook.react.bridge.ReactContext;
6
 import com.facebook.react.bridge.ReactContext;
6
 import com.facebook.react.bridge.WritableMap;
7
 import com.facebook.react.bridge.WritableMap;
7
 import com.facebook.react.modules.core.DeviceEventManagerModule;
8
 import com.facebook.react.modules.core.DeviceEventManagerModule;
20
     ReactContext mCtx;
21
     ReactContext mCtx;
21
     String mTaskId;
22
     String mTaskId;
22
 
23
 
23
-    RNFetchBlobFileHandler(ReactContext ctx, String taskId, Callback onResponse) {
24
-        // save temp file to RNFetchBlobCache/dltmp${taskId}
25
-        super(new File(RNFetchBlobFS.TempFilePath + taskId), false, false);
24
+    RNFetchBlobFileHandler(ReactApplicationContext ctx, String taskId, Callback onResponse) {
25
+        // save temp file to application storage
26
+        super(new File(RNFetchBlobFS.getTmpPath(ctx, taskId)), false, false);
26
         this.onResponse = onResponse;
27
         this.onResponse = onResponse;
27
         this.mTaskId = taskId;
28
         this.mTaskId = taskId;
28
         this.mCtx = ctx;
29
         this.mCtx = ctx;