The react-native-file-stream
library provides a convenient way to manipulate file streams on Android.
To install the library, run the following command:
$ npm install react-native-file-stream --save
To link the library, run the following command:
$ react-native link react-native-file-stream
To read a file, follow these steps:
- Convert
base64Data
toArrayBuffer
.ArrayBuffer
contains the original data. - Convert
ArrayBuffer
data to text or any other type of data you want.
To write a file, follow these steps:
- Convert your origin data to
ArrayBuffer
. - Convert
ArrayBuffer
data tobase64Data
. - Invoke the
writeFileStream
method to write data.
import ReactNativeFileStream from "react-native-file-stream";
let bufferSize = 1024 * 127;
ReactNativeFileStream.readFileStream({
uri: "content://xxxx.png",
fileName: "xxxx.png",
fileSize: 120023
}, bufferSize, (event) => {
const data = event?.data;
const hasMore = event?.hasMore;
if (hasMore == false) {
// Finish reading.
}
if (data) {
// This is `base64Data`.
}
})
import ReactNativeFileStream from "react-native-file-stream";
// 1. Init file write stream.
let path = "file://xxxx";
ReactNativeFileStream.initWriteFileStream(path);
// 2. Append data.
let base64Data = "cGxlYXNlIGdpdmUgbWUgYSBzdGFydA==";
ReactNativeFileStream.writeFileStream(path, base64Data);
// 3. Stop writing file.
ReactNativeFileStream.stopWriteFileStream(path);
import ReactNativeFileStream, { DataConvertUtils } from "react-native-file-stream";
const base64DataStr = "cGxlYXNlIGdpdmUgbWUgYSBzdGFydA==";
const arrayBufferData = DataConvertUtils.base64ToArryBuffer(base64DataStr);
const base64Data2 = DataConvertUtils.arryBufferToBase64(arrayBufferData);
const unit8ArrayData = DataConvertUtils.base64ToUnit8Array(base64DataStr);
const base64Data3 = DataConvertUtils.unit8ArrayToBase64(unit8ArrayData);
If you find this project helpful, please give it a star on my Github Repo. Thank you!