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);
Related
I have a question. I know I can store an image as byte array in SQLite database. But this needs some extra CPU work, so I tried to store only the id (R.drawable.myImage).
I stored it as an integer and I can retrieve it back. I am setting my image into imageview like this:
ivSportIcon.setImageResource(getItem(i).getIcon());
But I get an error:
Resources$NotFoundException: Resource ID #0x8
Is this a wrong way? Should I store my image as a byte array? Thank you in advance!
I do not know how you are storing the resource ID, but 0x8 is not a resource ID.
Beyond that, bear in mind that resource IDs change with each build, and so the resource ID that you save today will be different tomorrow.
Instead, store some other identifier in the database, that you can use to decide which of your drawables to use.
The only reason to store the actual image data in the database would be if you plan to change the images in the future and you want the user to be able to use the old images that happen to be associated with database entries.
You could store the name of the Drawable as a String instead, and use this method to retrieve its id:
public static int getResourceIdForName(Context context, String drawableName) {
final Resources res = context.getResources();
final String packageName = context.getPackageName();
return res.getIdentifier(drawableName , "drawable", packageName);
}
This is a little inflexible however, as if you decide to change the name of your drawable in future, then you will also need to change the name in the database.
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.
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 am displaying an image in my ListView. I stored the image in my drawable folder R.drawable.image. Eveything is working great
AddObjectToList(R.drawable.image)
Howver now I decided to sore the location in my sqllite db as a string "R.drawable.image". I need to parse it into a integer so I did this:
AddObjectToList(Integer.parseInt(imageelist3.get(i).toString()
Getting this error
ComponentInfo{myproject}: java.lang.NumberFormatException: unable to parse 'R.drawable.image' as integer
Any idea how to resolve?
First of all, you have to understand the meaning of R.drawable.image
Here R is a class R of auto-generated java file from android application,
which contains other class class drawable in that image is a int variable.
Its a int value and you are storing it as string then converted it in int which is wrong.
Just store int values of R.drawable.image.
"R.drawable.image" is not already int? Try to save to database directly "R...." with out parse it to integer.
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.