Files to Base64 conversion using RNFS - android

Im trying to convert a file to base 64. for that im using react-native-document-picker and RNFS.
Problem: When files is picked thru
react-native-document-picker it gives me success and the path is temporary cache path. but when i try to convert it to base64 using RNFS it gives me that error.
it will pick file from iCloud but not which is showed as "On My iPhone" in file manager i think issue is its not pick images from local storage of iPhone.
ENOENT: no such file or directory, open '/private/var/mobile/Containers/Data/Application/A78E7E14-DBAC/tmp/com.xyz-Inbox/Scanned%20Document.pdf'
here's my code:
const response = await DocumentPicker.pickSingle({type: [DocumentPicker.types.allFiles],
copyTo: "documentDirectory" });
var data = await RNFS.readFile(response.uri, 'base64');
Thanks for help.

Related

How to export data into a specific extension ? like PDF

I'm trying to program an app that let users write their novels or notes with fun features like listening to piano and raining sounds while writing...etc. Everything works fine and great but the problem is how can i turn those novels to PDF? ... I store everything as a "String" data type...ofc the user want to save his work which obviously I can do (Local storage or firebase) however i don't know about exporting data as a specific extension. how can I export the data as a PDF file if the user wanted to do that? is there a general way to do it?
One possible solution can be pdf Flutter plugin. You can create and save a pdf file from strings. I tried this sample code and pdf is created:
//get the directory to save pdf file
late Directory? directory;
if (Platform.isIOS) {
directory = await getApplicationDocumentsDirectory();
} else {
directory = await getExternalStorageDirectory();
}
var path = directory?.path;
//create pdf file
final pdf = pw.Document();
pdf.addPage(
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Text('Hello World!'),
),
),
);
//save pdf file
await File('$path/test.pdf').writeAsBytes(await pdf.save());
You can use path_provider Flutter plugin to get the directory location

Android Doesn't Recognize Image Downloaded by Flutter App

Android Flutter app downloads an image from a server on another Android phone. After downloading, the file is unable to be opened or viewed by the phones gallery or file explorer. However, when examined by a hex editor, the downloaded file and the original file are exact copies, and when imported to windows, the "corrupt" downloaded file is view able by the Image Viewer. The only difference I could find between the files was the metadata examined by Windows. What could be the cause of this?
Original File on Android Server:
Downloaded File On Android Client:
Here's the code I'm using to create the file from a Uint8list:
Future<File> downloadFileAndroid(Uint8List fileBytes, String fileName) async{
var dir = await getExternalStorageDirectory();
File photoFile;
var photoDirectory = await Directory(dir.path + "/Downloader").create(recursive: true);
photoFile = await new File(photoDirectory.path + "/" + fileName).create();
if(await photoFile.exists()){
await photoFile.writeAsBytes(fileBytes);// also tried flush: true
print("Created file and it exists");
} else {
print("Error: tried to create file but it doesnt exist");
}
}
I faced this problem when downloading an image from the internet and setting a png extention to it. Using an extention on the file name usually causes this error. Consider using https://pub.dev/packages/image_downloader to download the image using the default function in the example code. With the default option, the extension is not determined by the coder but instead by itself.

How can I change my local image to blob file type in React Native?

I want to change my local image into blob file type. How can I do it with react-native-fs.
This is what I tried below
RNFS.readFile('../../assets/imgs/profile.jpg', 'base64')
.then(res =>{
console.log(res);
alert("res");
});
This is what I'm trying so far but it gives me a warning - "
no such file or directory, open '../../assets/imgs/profile.jpg'
You can use rn-fetch-blob library
The problem is this you are going to access the system directory.
../../assets/imgs/profile.jpg
react-native-fs give access to the native filesystem.
the image should be placed on your mobile and give the right image path
if the image inside the document directory so you can access the document path using
RNFS.DocumentDirectoryPath + '/profile.jpg'

Uploading file with the uri starting with content://

I am reading a file from android and uploading it to a remote server.I am getting the file in the form of
content://com.android.providers.downloads.documents/document/3
Since i am using react-native, can the uri above used as a file path?.
I can get the exact file path but when the file picker gets the file and the file has spaces, the picker cannot read the file but the uri takes into account spaces.
What can be done to use content://com.android.providers.downloads.documents/document/3 as the file path?.
If i understand what you want , its something like this
var x = "content://com.android.providers.downloads.documents/document/3"
const file = {
uri: x,
name: "nameFile",
}
const formdata = new FormData();
formdata.append('Files', file);
fetch('URL', {
...

as3 air get specific path to file on device

I am using as3 air and testing on an android.
I need to able to load, thru a browse feature, the sd card
OR
I need to point directly to that file folder thru a path.
I have successfully downloaded a zip file from a server. I can unzip it while testing on
my desktop because I can browse to the correct path on desktop but not on phone. But I don't need the browse feature anyway, right now. I just need to be able to point to the applicationStorgeDirectory and the file in there. :)
I need the file var :)
var reader:ZipFileReader = new ZipFileReader();
reader.open(file); /// Help :)
UPDATE Sept 4th 2012 5pm
I tried this code:
var file:File = File.applicationStorageDirectory.resolvePath("myFile.zip");
trace(file.url); //path to the file
//assuming that function takes a string path
reader.open(file.url);
and I got this error:
Scene 1, Layer 'Layer 1', Frame 1, Line 46
1067: Implicit coercion of a value of type String
to an unrelated type flash.filesystem:File.
This should give you a url to an existing file
File url
You could try something like this:
var file:File = File.applicationStorageDirectory.resolvePath("myFile.zip");
trace(file.url); //path to the file
//assuming that function takes a string path
reader.open(file.url);
Try the following. I've tested.
var file:File = File.applicationStorageDirectory.resolvePath("myFile.zip");
trace(file.url);
reader.open(file);

Categories

Resources