In one activity, there are 1 text view and 1 image view. I have to display number of data for different images. I have stored the data in string-array in strings.xml. My code is working fine for them. Problem is with images. I have stored images in R.raw folder. How can I get the images in image view so that for every data corresponding images can be displayed.
In the listactivity(where the data are displayed in a listview i have added async task and in doinbackground i am loading values from these arrays ). I am stuck in images. How to do that? pls help...
started like this,
int image[]= {getResources().getIdentifier("aa", "raw", getPackageName()),
getResources().getIdentifier("bb", "raw", getPackageName()),
getResources().getIdentifier("cc", "raw", getPackageName()),};
Pls reply..
Many Thanks
Why are the images in Raw? Put them into /drawble-hdpi or /drawable-mdpi...whatever and then set the image by myImageView.setImageResource(R.drawable.myimage) and then associate it with your data however you want...
For resources in your raw folder, you should be able to use Bitmap.decodeResource to obtain an image you can pass to the ImageView.
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 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.
is it possible to avoid storing images in a database and putting them into the drawable or mipmap folders instead? In the database then I would like to put only the reference to ressources and call it like this:
String image = "R.mipmap.image1"; //This will be the data retrieved from Database
int imageInt = Integer.parseInt(image);
imgview.setImageRessource(imageInt);
Actually I want to use this in a RecyclerView so I simplified the example code here but this is showing me the following error
java.lang.NumberFormatException: Invalid int: "R.mipmap.image1"
Thanks
One simple way is to store the image name in database (i.e. "image1" in your example). Then later get the identifier for it using image name as below:
int imageId=context.getResources().getIdentifier("imageName","mipmap",context.getPackageName());
Now, you can use this imageId for setting image to image view.
imgview.setImageRessource(imageId);
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.
I want to make a listview with thumb images in each row and when I stock those images in the database, the querying process became very slow and uneffective, so I am now considering to save a path to the images in assets folder or res folder instead,can anyone tell me how to do that, thanks
An example or tutorial would be much appreciated.
Try use getAssets() method from Context. http://developer.android.com/reference/android/content/Context.html#getAssets()
Hope, it help you!
Why can't you just save the file name?
Then assuming you know the path just load the filename from the database and add it to the path when loading the image. I am doing this to load images from the net.
If you are trying to load a resource you could save the int value that is used to identify the resource.