Just a quick message to ask you about how to store images URLs in Firebase Database for later use.
Let me explain:
I use Firebase Storage to store Images but store their respective URLs in Firebase Database, separately from Firebase Storage (as I have been reading on here it was the way to do it).
So in Firebase Database I have a "ItemPictures" Node where I want to store up to 5 pictures URLs per Item.
But I don't know what is the best way to store multiple image URLs of a same Item:
- Creating a List with random ids using push() (and then having to manage deleting manually the picture'sURL when it gets updated).
- Creating a "Picture" Object with 5 URLs String values, making it easier to locate and navigate through.
The thing is that later on I will need those URLs to populate a View Pager with the 5 pictures.
Cheers,
Andy
using an ArrayList will be a better one
ArrayList<String> images = new ArrayList<>();
images.add("img1url");
images.add("img2url");
images.add("img3url");
images.add("img4url");
images.add("img5url");
The simplest way is to store each image object with unique key and inside it have five keys with values 1,2,3,4,5 respectively. Then you can easily fetch the desired URL for particular image with ID by :
firebaseSnapshot.val().ID.1;
firebaseSnapshot.val().ID.2;
firebaseSnapshot.val().ID.3;
firebaseSnapshot.val().ID.4;
firebaseSnapshot.val().ID.5;
To further improve the performance you can index the image IDs as they are going to grow larger in number. This has been done in the following question.
enter link description here
Related
I am making an application and I want one of the Fragments to have an ImageView and the image to be taken from a URL that connects to my VPS. I would like you to tell me how can I take a random url from the img folder of the url https://imagerandom.com/img (that url is an example) and project it to ImageView
Method 1
If you already have a bunch of images you can follow below steps:-
1)first import the all images in the file/code.
2)then store their names in the array or list of string/integer depending upon the name type.
3)then uses the random function to generate index of list/array.
4)then store the random index in some variable and you can use that to have a random images.
Method 2
On internet a lot of websites are available for generating random pictures
I used "picsum" and help me a lot in various web application for testing
Link for the site:-
https://picsum.photos/
hope you gonna love it !!
I have an app that generates objects of a class, let's call it X, that is Serializable. I want the user to be able to occasionally save or delete objects of X from his/her list of favorites (a list that can go up to 100 objects).
What is the most appropriate way to persistently store the list of favorites?
In SharedPreferences, storing the whole list as a JSON String
In an SQLite database:-
Storing one item per object, as BLOB's
Storing one item per object, as JSON Strings
In a custom file, storing the whole list as a JSON String
Some other way?
My thoughts are:
Because adding and removing favorites will be occasional, and the list is small, I probably don't need the advantages of a DB when it comes to searching fields in large amounts of data. So I am inclined to maintain a local ArrayList, add and remove items from it, and save it to SharedPreferences (option 1).
It seems odd to save a key-value pair holding an entire list as a JSON String, I'm afraid I might be unaware of some sort of limitation.
Is there a limit to the size of the String I can store in SharedPrefferences?
Is it too problematic that I add or remove objects from my local ArrayList and then save the whole list?
I am agree with your first approach because of It's manage easily and need small storage space.
I have an idea about the widget that when I click on widget, the text & image will refresh randomly. I have done this with text, but my images are stored on Firebase and I want to take these random images and display them in an ImageView. So how I can do this?
Screenshot of my Firebase Storage:
Screenshot of my App:
As an alternative to #Bernd's answer: You could modify your image names to a standard naming scheme using incremental numbers. You could then retrieve the image URLs dynamically, like so:
Example image names:
image_0.jpg
image_1.jpg
image_2.jpg
image_3.jpg
Some example Java code to generate a random image filepath:
//The amount of images you have stored
int maxImages = 4; //Amount of images
Random random = new Random();
//Randomly generate a filepath to an image
String imageFilePath = "image_" + random.nextInt(maxImages) + ".jpg";
You can then use your generated imageFilePath with FireBase Storage's getDownloadUrl() to retrieve the proper download URL. You can then pass the URL to Glide, Picasso, or another image download library to load it into your ImageView.
Advantages
Only have to use Firebase Storage to achieve your goal
Less overhead on the database, don't have to maintain a list of images there
Disadvantages
You have to control the image names tightly, no custom image names
You have to have a fixed number of images
Could break if you delete an image without changing other image names
Retrieving the URL will throw an exception if the image couldn't be found (e.g. if the random number is out of bounds)
To randomly select an image from Firebase storage, you need to have a list of download url's of the files somewhere. As described in this answer, there is no Api to get a list of these pictures at the moment, so you will have to store the urls of them somewhere.
One possibility for this is to simply add the urls
of the files to Firebase database.
When you want to select a random image, you could go through the database,
and select a random url from this list.
To actually display them in the ImageView, you can use a library like Glide, to make this process easier. The FriendlyPix example App by Firebase, shows how to do this.
In my app, the user can update some data. For example, the images associated. So, I made a db query to know the paths of the images that are already in the Database. I stored the results in a String ArrayList.
I have another custom type ArrayList, BeanFotos. This type has a String which is the path to the images, including the ones that are in the database AND the images which the user just took with the camera.
So, I need to store the images that are not in database, i mean, the one that the user just took.
I am trying this way:
for(int i=0;i<listaFotos.size();i++){//listaFotos is the BeanFotos ArrayList
String ruta=listaFotos.get(i).getFotoPath();
for(int j=0;j<fotosBBDD.size();j++){//fotosBBDD is the String ArrayList
if(ruta.equals(fotosBBDD.get(j))){
listaFotos.remove(i);
}
}
}
storeInDb(listaFotos);
This way, I get unexpected result: One image that already is in the Database, is stored again. How could I do it properly?
Thank you.
To avoid the Redundancy of data use HASHMAP http://www.tutorialspoint.com/java/java_hashmap_class.htm instead of ArrayList.
Right now I have a simple App that takes a picture and saves it to a folder. I'm trying to figure out a way to have a user answer questions about each picture (I would just be using EditTexts with that). Is there a way to connect those EditTexts with the image so that when I would open up the image on another page that i could click a button to view the information that was entered along with that image? I have an idea on how to code everything else, just not keeping the edittexts associated with the image. Any ideas would be greatly appreciated!
Thank you!
perhaps you could create a data file alongside the image that has the same name as the image file, but a different extension.
So if you are saving an image called img1.png you save another file alongside it called img1.txt which contains the data you collect from the EditText(s). How you format this data file will depend a bit on how much data you have to store, and what kind of structure the data needs to represent (if any)
Then when you load up a png to display it you'll need to also load up the txt file that shares the same name, and populate the data contained inside to some TextView or something near your ImageView.
Use a database. Make a table that associates images with the data you want. When you display the picture, read the info for that picture from the database and display that as well. Either store the images directly in the db or store them in a folder and use the filename as the primary key in the database table.
There's number of ways to accomplish this. You could use a hashmap where the String is the data they entered and int is the resourceID of the image. For persistent storage, I recommend an SQLite Database that stores the string in a column and the associated image in another.
A few options:
Save a file with the image.. Give it the same name but .txt or something. The file should probably be stored in the location returned by getFilesDir() . Alternatively you could use SharedPreferences.
Use an sqlite database
Save within the exif data of the image. Use the ExifInterface class with setAttribute/getAttribute to save the values.