diff --git a/android/src/main/java/com/rnfs/RNFSManager.java b/android/src/main/java/com/rnfs/RNFSManager.java index a47a3f41..28ffb798 100644 --- a/android/src/main/java/com/rnfs/RNFSManager.java +++ b/android/src/main/java/com/rnfs/RNFSManager.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.ArrayList; +import android.net.Uri; import android.os.Environment; import android.os.AsyncTask; import android.util.Base64; @@ -14,10 +15,8 @@ import java.io.File; import java.io.FileOutputStream; -import java.io.FileInputStream; -import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.io.OutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; @@ -47,8 +46,57 @@ public class RNFSManager extends ReactContextBaseJavaModule { private SparseArray downloaders = new SparseArray(); + private static ReactApplicationContext mreactContext; + public RNFSManager(ReactApplicationContext reactContext) { super(reactContext); + mreactContext = reactContext; + } + + /** + * get bytes array from Uri. + * + * @param context current context. + * @param uri uri fo the file to read. + * @return a bytes array. + * @throws IOException + */ + private static byte[] getBytes(Context context, Uri uri) throws IOException { + InputStream iStream = context.getContentResolver().openInputStream(uri); + try { + return getBytes(iStream); + } finally { + // close the stream + try { + iStream.close(); + } catch (IOException ignored) { /* do nothing */ } + } + } + + /** + * get bytes from input stream. + * + * @param inputStream inputStream. + * @return byte array read from the inputStream. + * @throws IOException + */ + private static byte[] getBytes(InputStream inputStream) throws IOException { + + byte[] bytesResult = null; + ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + try { + int len; + while ((len = inputStream.read(buffer)) != -1) { + byteBuffer.write(buffer, 0, len); + } + bytesResult = byteBuffer.toByteArray(); + } finally { + // close the stream + try{ byteBuffer.close(); } catch (IOException ignored){ /* do nothing */ } + } + return bytesResult; } @Override @@ -75,15 +123,13 @@ public void writeFile(String filepath, String base64Content, ReadableMap options @ReactMethod public void readFile(String filepath, Callback callback) { try { - File file = new File(filepath); - - if (!file.exists()) throw new Exception("File does not exist"); - - FileInputStream inputStream = new FileInputStream(filepath); - byte[] buffer = new byte[(int)file.length()]; - inputStream.read(buffer); + Uri uri = Uri.parse(filepath); + if (uri.getScheme() == null) { + uri = Uri.parse("file://" + filepath); + } + byte[] inputData = getBytes(mreactContext, uri); - String base64Content = Base64.encodeToString(buffer, Base64.NO_WRAP); + String base64Content = Base64.encodeToString(inputData, Base64.NO_WRAP); callback.invoke(null, base64Content); } catch (Exception ex) {