Commit 3e19180c authored by jlok's avatar jlok Committed by Mehdi Achour

fix: safe guard getFreeDiskStorage() and getTotalDiskCapacity() (#322)

parent d03f20a1
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
### Next ### Next
* Fix crash on `getFreeDiskStorage` and `getTotalDiskCapacity` from invalid filesystem path (https://github.com/rebeccahughes/react-native-device-info/issues/320)
### 0.15.1 ### 0.15.1
* Fix Android compatibility for `getFreeDiskStorage` and `getTotalDiskCapacity` (https://github.com/rebeccahughes/react-native-device-info/pull/319) * Fix Android compatibility for `getFreeDiskStorage` and `getTotalDiskCapacity` (https://github.com/rebeccahughes/react-native-device-info/pull/319)
......
...@@ -127,15 +127,25 @@ public class RNDeviceModule extends ReactContextBaseJavaModule { ...@@ -127,15 +127,25 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
} }
@ReactMethod @ReactMethod
public int getTotalDiskCapacity() { public Integer getTotalDiskCapacity() {
try {
StatFs root = new StatFs(Environment.getRootDirectory().getAbsolutePath()); StatFs root = new StatFs(Environment.getRootDirectory().getAbsolutePath());
return root.getBlockCount() * root.getBlockSize(); return root.getBlockCount() * root.getBlockSize();
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }
@ReactMethod @ReactMethod
public int getFreeDiskStorage() { public Integer getFreeDiskStorage() {
try {
StatFs external = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); StatFs external = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
return external.getAvailableBlocks() * external.getBlockSize(); return external.getAvailableBlocks() * external.getBlockSize();
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment