React native how to save image in application? - reactjs

I want to save images from camera roll internally in my app.
I've been using react-native-image-crop-picker to import the image's base64 data and saving it with AsyncStorage.
The problem is when it comes to render Images using base64 data, it's extremely slow.
I would like to save the images and have their URIs in order to render them faster.

Refer this link How to get absolute path of a file in React-native
This Link will help you in Implementation Code Sample
Once you get the absolute path of the file rn-fetch-blob package will help you store it your file system checkout this link File system
I hope this helps, do comment if any doubts !

Related

Displaying both PNG and JPEG files in React

I think this is a repeat question but I can't seem to find the right answer...
I can upload png and jpeg images just fine (mern with multer) however when it comes to displaying the PNG file I have some issues.
File location is in the public/uploads folder
Successful display of JPEG (Displays JPEG correctly with the following) but fails on PNG
<img src={`${post.image}`} alt="testing"></img>
Error when it comes to PNG (404 Not Found)
GET http://localhost:3000/uploads/postImage-1637006445002-134875838-Screen%20Shot%202021-11-15%20at%205.25.09%20PM.png 404 (Not Found)
If I try the following, I get another kind of error for both JPEG and PNG
<img src={require(post.image).default} alt="testing"></img>
Cannot find module '/uploads/postImage-1637006445002-134875838-Screen Shot 2021-11-15 at 5.25.09 PM.png'
Doing an import such as below doesn't really make sense since I'm pulling the image name from a database and then referencing it's location in the public/uploads folder
import Image from 'uploads/fileName.png';
My other option is to a fileFilter on the backend and limit uploads to just JPEG but that seems limiting.
The only difference between the two that I can tell is the file type. Backend file handling is the exact same, so my question is how do I display PNG images the same exact way that I display JPEG's? Is there some config with React that I have to change?
Any help would be much appreciated. Thank you,
Your web server may be setup to receive the PNG mime type, but not to send files of this type, which would account for the 404 error you are receiving when you try to render it. Each web server is different in how this is configured.
I didn't know that spaces in the file name when uploading wouldn't get pulled properly by React. If you're running into this, what fixed my issue was removing spaces from the filename. Thanks to Steve -Cutter- Blades for pointing it out.

Does my mobile app need a database or should I save everything to files instead?

I have created a video editor using React Native. I need to choose a way to save all of the user's projects locally. Each project has a video file, thumbnails (images) and its current Redux state.
My first idea is to save everything in files using RNFS. Each Project's folder would have a video file, Thumbnails folder and a state.txt file containing my application's current state (current text size selected, background color, etc...).
Do I need a database like SQLITE or should I save everything in files? I know I'll have to use RNFS for the binary data like videos and images. But what about the state.txt file? is that a good idea? The idea of each user having their own local database just for that sounds strange to me.
You have your Redux state that just represents local state. Have a look at https://github.com/rt2zz/redux-persist
This allows you to serialize the Redux State and save it using whatever storage you provide, but you can use asyncStorage in the react native apps.
You are right about using RNFS for videos etc. For the metadata in state.txt, in my opinion, it would be better to go with async-storage or other offline storage options (like Realm )
It will be much more performant to query it instead of reading from a file.

Serving dynamically generated files from GitHub Pages

I'm a few months into web development so I apologize if I misunderstood anything.
What I did
I created a react-random-shapes package that would draw out random shapes as a React component. You can see an example here on my site or in the project page. Each time you refresh the page you'd get a new image. (Note: these pages use React.)
What I want to do next
The result I'm aiming for is to create an API (GET-only) on GitHub Pages that would return the dynamically generated svg file (so you can do something like
<img src="https://github.com/artt/react-random-shapes/blob?size=300&fill=red">
which would return a random blob for anyone who's interested in using. Alternatively, this API could return the svg path so the user could do whatever they want with it (e.g. animation).
The problem I have
Right now I know how to output an html page with the svg file, but not quite sure how to return just the svg (or json, etc.) part of it.
Thanks!
I am trying to do the same thing. I think your best bet would be to use a webserver on another platform like Heroku or, another good option, Replit.

display and save a local image into database in react js

I'am developing a blog website using react.js where users can post and comment ...
so we know that a user can comment with an image ,and let's say he want to upload a local image that stored in his machine,i want to display that image as a comment and save it into postgres database which i'am using for that.
My question is what are the steps that i should follow to achieve my purpose,thanks in advance.
If you have to do in simple steps, you can save the image to base64, its just string representation of an image. So it can be saved in DB directly and when you have to show it just decode the string and display it accordingly.
References :-
https://github.com/dankogai/js-base64#readme
https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript
Alternatively, what you can do when user selects an image, then upload that image to your server and get the path to that(multipart file upload) and from path you can show it where you want to show.
References :-
how to send a multipart/form-data from React.js with an image?
I hope it helps, Thanks :)

How to store images in firebase using URL from cloud storage service?

Hi I am developing an ionic app using firebase as my database.
I read this question.
I understand it and have a more specific question.
I uploaded an image on amazon s3, imgur and filestack, and tried all three in firebase using data:url/<the url of image> and data:image/jpeg;<url of image>.
Am I typing something wrong into firebase?
The image does not come up. I do not want to write code in the app to upload the image etc, I just simply want an image to display on the app beside some data corresponding to an item in my firebase database.
I have read about base64, do I have to use it in this case?
(Optional) Perhaps If you could expand on the steps of doing this:
"You can use a filepicker to get the image file. Then you can host the image however you want, i prefer amazon s3. Once the image is hosted you can display the image using the url generated for the image."
I don't have much ionic experience, but why not just save the direct path to the image url and not include the data:url/ portion.
Simply store the url in firebase and then when it's retrieved inject it into your img src.

Resources