• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image is not displayed

 
Alex Munozz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to display an image in an ImageView. I have several records in the database with a title, a description, the latitude, the longitude and an image of the place. I watch the title and the description, but not the image. The rest is not necessary to display it.
The image is not in the database, is caught in the gallery and its uri is stored as a String.

Data are displayed using a ListView.

Here is the code:

Layouts:

For every item of the ListView:



For the ListView:



The adapter:



To save items in the ListView first I retrieve data from a cursor and I insert them in an ArrayList of type of a class named Lugar.





When I run the project I don't watch any error in the logcat. The only problem I have is that the photo is not displayed.

How can I display the image?


Thanks.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Adapter, starting at line 57, you get an item and call it lugItem, and all the properties that work seem to reference that variable. But the image URI you retrieve in line 61 comes from the class member lgu. It looks like lgu is a dummy instance that holds no data, and I don't see it change anywhere. So I think the problem is you are getting the URI from the wrong instance of Lugar, and line 61 should change to rutaimg = lugItem.getfotolugar();. Is there a reason the class member lgu exists?
 
Alex Munozz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I declared lgu is because I wanted to access to functions that are in the Lugar class. But in this situation it's not necessary.
I changed lgu for lugitem but I can't watch the photo.
I checked the value of rutaimg and it's different in every record of the database. Well, this is not exact. I have three records in the database: two without image and one with image. When I wanted to check the value of rutaimg that is the path of the image, the records of the database that hasn't image had the value: com.app/mapa/2130837504 and the other without image has not value and I don't know why.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am not sure I understand the state of things now. If the values returned from the database are wrong, you have to clean up the database and check the code which puts data in the db to see if it does it correctly. If the value in the database looks right, but no image shows up, then you have to check to see if that file exists and if it is an actual image.
 
Alex Munozz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But I know all the values in the database are right, the problem is that I don't know how to retrieve the image. The idea is to insert the title, the description and the image in the database. But the image is not stored in the database because it's very heavy for the memory, so I save the path of the image. The image is obtained from the gallery only, not from camera.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the values you get from the the database for the image URI are correct or not? You said:
When I wanted to check the value of rutaimg that is the path of the image, the records of the database that hasn't image had the value: com.app/mapa/2130837504 and the other without image has not value and I don't know why

What does that mean? The way I read it is:
For one entry in the database that does not have an image, the value assigned to rutaimg was com.app/mapa/2130837504.
For the second entry in the database that does not have an image, there was no value assigned to rutaimg. (null?)

This sounds like the values you are getting from the database are incorrect - one of them has a value, the other one doesn't, and you expect neither of them to have a value. If the value in rutaimg is not correct then how can you say you know the value in the database is correct? Have you used some other tool to inspect it directly?

Or maybe I mis-read what you wrote.
 
Alex Munozz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only value that is not displayed correctly is the image and I know it because I don't watch anything.
When I insert one record in the database and I want to list the whole, I can watch all data except the image. Which value has rutaimg? Blank. So I know that the path for the image is not correct.

I think where is the error but I don't know how to fix it. It's in the value of the image's path when I insert a record in the database.

When I insert the image in an ImageView I want to obtain the path of this image and I do it as following:



Here is the xml file:



The ImageView where I insert the image is the second one, that has imgFoto id.
I insert the image selecting it from the gallery and what I want to do is to save the image's path in a String and when I list all records of the database I become the String path into Uri.

When I retrieve all the records in the database I can watch that all image values are the same.

How can I save the path of a selected image correctly?

Thanks.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The image you 'saving' @drawable/camera1. This is not an image that is in the gallery, it is an image in the application's drawable folder(s). Also, you use the ID in R.drawable, which is an integer constant used to represent a drawable resource in code - it is not the address of the image and it is not safe to use for storing or retrieving images (you run the risk of overwriting an image that already had been saved, and if you change versions you may have a different ID so would not be able to find the image.)

So what image are you actually trying to load/store? Where does it exist? How do you load it initially?
 
Alex Munozz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The image I want to store is one selected by the user from the gallery. The user click on the ImageView and then he can choose insert an image from the gallery or from the camera. He must select from the gallery because it's the only option I have coded now. When the user has selected the image it is showed in an ImageView.

The user click on the save button and the title, the description and the image are saved in the database. But the image is not stored in the database, it's its path in a String format. But I don't know if it's possible to do something like this. I think so. The problem is that every time the user select an image it's a different one and I don't know how to store the path in the database.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Munozz wrote:The image I want to store is one selected by the user from the gallery. The user click on the ImageView and then he can choose insert an image from the gallery or from the camera. He must select from the gallery because it's the only option I have coded now. When the user has selected the image it is showed in an ImageView.


Okay, so at some point the user selects an image and you get the URI back as a result, and you use that URI to load the image into the ImageView, correct? Then what you need to do is store the URI that you get as a response to the user action and put that in the database. You will not be able to re-create that URI so you need to store it in a variable and when needed, put it in the database.

Pseudo code:
 
Alex Munozz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. But how can I save the Uri of the image selected?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, you were too quick to respond I edited my post with pseudocode to demonstrate.

To answer the question: you put it in a variable someplace - or if you are really safe, you have a database table that you immediately store the URI to as soon as the user selects it. Then when the user Saves you simply copy from one location in the DB to another.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic