Last active
April 23, 2020 12:17
-
-
Save Abby3017/24f9526581b1b92642b0360756467c5e to your computer and use it in GitHub Desktop.
In React Native directory move is not provided, react-native-fs and react-native-fetch-blob fail to provide any interface for that. This is native way to do that
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
compile 'commons-io:commons-io:2.5' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abhinav; | |
import com.facebook.react.ReactPackage; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.uimanager.ViewManager; | |
import com.abhinav.FileModule; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class FileManager implements ReactPackage { | |
@Override | |
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { | |
return Collections.emptyList(); | |
} | |
@Override | |
public List<NativeModule> createNativeModules( | |
ReactApplicationContext reactContext) { | |
List<NativeModule> modules = new ArrayList<>(); | |
modules.add(new FileModule(reactContext)); | |
return modules; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abhinav; | |
import com.facebook.react.bridge.Promise; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import org.apache.commons.io.FileUtils; | |
import java.io.IOException; | |
import java.io.File; | |
public class FileModule extends ReactContextBaseJavaModule { | |
public FileModule(ReactApplicationContext reactContext) { | |
super(reactContext); | |
} | |
@Override | |
public String getName() { | |
return "Directory"; | |
} | |
@ReactMethod | |
public void moveDirectory(String src, String des, Promise promise) { | |
File srcDir = new File(src); | |
File destDir = new File(des); | |
try { | |
FileUtils.deleteQuietly(destDir); | |
FileUtils.moveDirectory(srcDir,destDir); | |
promise.resolve(true); | |
} catch (Exception e) { | |
promise.resolve(e.toString()); | |
} | |
} | |
@ReactMethod | |
public void deleteDirectory(String dir, Promise promise) { | |
File delDir = new File(dir); | |
try { | |
FileUtils.deleteDirectory(delDir); | |
promise.resolve(true); | |
} catch (IOException e) { | |
promise.resolve(e.toString()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainApplication extends Application implements ReactApplication { | |
private static CallbackManager mCallbackManager = CallbackManager.Factory.create(); | |
protected static CallbackManager getCallbackManager() { | |
return mCallbackManager; | |
} | |
@Override | |
protected List<ReactPackage> getPackages() { | |
return Arrays.<ReactPackage>asList( | |
new FileManager(), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm in a wierd situation, i'm able do delete my directories (tested chaing here FileUtils.deleteQuietly(destDir); on moveDiretory method)
But the files are not moving, even tried copyFile aswell :\ what could be wrong?
did it! with FileUtils.moveFileToDirectory(srcDir,destDir,false); and installed the apk on top of my native app, its working. Thank you very much