react-native-image-resizer icon indicating copy to clipboard operation
react-native-image-resizer copied to clipboard

output to base64 string

Open fgrs opened this issue 8 years ago • 9 comments

is this currently possible?

fgrs avatar Aug 23 '16 21:08 fgrs

Not currently, but you can use a module like react-native-fs to read the output file that was created by react-native-image-resizer. See readFile method documentation. Beware of large files, it can be quite hazardous to store such files in a base64 string.

4ian avatar Aug 23 '16 22:08 4ian

I did it the way you suggested but now for image resized and saved in

file:/data/data/com.app/cache/1472039430399.JPEG

I get following error in readFile:

Error: ENOENT: no such file or directory, open 'file:/data/data/com.app/cache/1472039430399.JPEG'(…)

fgrs avatar Aug 24 '16 12:08 fgrs

is this path correct? file:/data/data/com.app/cache/1472039430399.JPEG

fgrs avatar Aug 24 '16 12:08 fgrs

Not sure, you should double-check if the file is really on the device or not using for example the android tools in Android Studio :)

4ian avatar Aug 24 '16 16:08 4ian

Maybe too late for you, but it can help others. storing local files on real devices will give dynamic document/caches directories. To find the correct app directory, us react-native-fs:

var RNFS = require('react-native-fs');
var filepath = RNFS.DocumentDirectoryPath + '/'+yourfilename;
var readfile = RNFS.readFile(filepath, 'utf8');

the 'DocumentDirectoryPath' will give you your current documentpath.

virgial avatar Sep 05 '16 12:09 virgial

Any update?

LucasMonteiro1 avatar Nov 23 '17 17:11 LucasMonteiro1

If anyone runs into this, @virgial is mostly correct, although you need to use

var readfile = RNFS.readFile(filepath, 'base64')

instead of utf8.

https://github.com/itinance/react-native-fs/issues/417

Here's my working example:

           const resizedImageUri = await ImageResizer.createResizedImage(
            `data:image/jpeg;base64,${response.data}`,
            400,
            400,
            "JPEG",
            100,
            0
          );
          const base64Data = await RNFS.readFile(
            resizedImageUri.path,
            "base64"
          );
          this.props.onPhotoUpload(`data:image/png;base64,${base64Data}`);

peterholcomb avatar Jun 21 '18 12:06 peterholcomb

This works: 'data:image/jpeg,'+image The code expects a comma before the base64 image, for some reason...

mickael-h avatar Mar 06 '19 16:03 mickael-h

@mickael-h Your solution is not suited for the question. You might be replying for 'how to upload base 64'

iam-joel avatar Oct 07 '19 18:10 iam-joel

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 01 '22 19:09 stale[bot]

This issue has been automatically closed. Thank you for your contributions.

stale[bot] avatar Sep 09 '22 03:09 stale[bot]