Download Images using stored URL's

Hi. I’ve been endlessly trying to make this work. My users upload a bunch of images, all getting their unique URL that is stored in a Table on Backendless. It is performed this way when uploading an image:


Backendless.Files.Android.upload(image1_scaled, Bitmap.CompressFormat.PNG, 100, “image1”, “images”, new AsyncCallback<BackendlessFile>() {
@Override
public void handleResponse(BackendlessFile response) {
fileMapping.url_1 = response.getFileURL();
Backendless.Data.of(UserFileMapping.class).save(fileMapping, new AsyncCallback<UserFileMapping>() {
@Override
public void handleResponse(UserFileMapping response) {
toast_error(“Image stored”);
}
@Override
public void handleFault(BackendlessFault fault) {
}
});
}
@Override
public void handleFault(BackendlessFault fault) {
}
});
And I can confirm that the url is indeed stored in the table.
But when I try to fetch it before downloading the file:


 Backendless.Data.of(UserFileMapping.class).find(new AsyncCallback&lt;BackendlessCollection&lt;UserFileMapping&gt;>() {


 @Override
 public void handleResponse(BackendlessCollection&lt;UserFileMapping&gt; fileMappings) {


 String userToken = HeadersManager.getInstance().getHeader(HeadersManager.HeadersEnum.USER_TOKEN_KEY);


 Iterator&lt;UserFileMapping&gt; iterator = fileMappings.getCurrentPage().iterator();


 while (iterator.hasNext()) {
 UserFileMapping fileMapping = iterator.next();
 image1_url = fileMapping.url_1;
System.out.println("URL" + image1_url);
 }


 }


 @Override
 public void handleFault(BackendlessFault fault) {
 }
 });

It floods the logcat with my SysOut of the url with “URL null” and occasionally it prints out the correct url in the flood of SysOut messages, but when the flooding stops it has always “null”.
So firstly, why does the .find() method keep executing multiple times? And secondly, why does it seem to find the correct URL only sometimes during the time .find() is executing but always ends up as NULL?
This is what my logcat shows me:
I/System.out﹕ URLnull
I/System.out﹕ URLnull
I/System.out﹕ URLnull
URLhttps://api.backendless.com/xxxxxxxxxxxxxxxxx/v1/files/images/image1
URLhttps://api.backendless.com/xxxxxxxxxxxxxxxxx/v1/files/images/image1
I/System.out﹕ URLnull
I/System.out﹕ URLnull
I/System.out﹕ URLnull
etc…
So when I run a HTTP GET request on the URL , it takes about 50 seconds before it has fetched the image, presumably because the method is busy providing the URL or something?

Hello,

On the file upload side, do you realize that you’re storing all the uploaded files as the same file, meaning any additional upload overwrites the file you already have? Take a look at the upload call:

Backendless.Files.Android.upload(image1_scaled, Bitmap.CompressFormat.PNG, 100, "image1", "images", asyncCallback )

The file is uploaded into the directory called “images” and stored with the name “image1”. A directory cannot have two files with the same name. You can confirm it using Backendless Console. Click the “Files” icon and then navigate into the “images” directory.

As for the find API, what makes you think that it is invoked multiple times? One other question, have you verified what the data looks like in Backendless console in the UserFileMapping table?

Regards,
Mark

Hi Mark.

The upload method provided was just an example, I have 12 different method’s for each image upload, given different names but in the same directory.

I was wrong about it being invoked multiple times, my IDEA played me a trick on that one.
Well I’ve checked the FileMapping table and the URL is there, and using the url from console I receive the correct image, so it’s all stored correctly. But when I iterate through the table in code, it’s given back as NULL for some reason.

Could you please attach a screenshot of the UserFileMapping table?

I’ve uploaded three images so far during my testing, everyone successfully got their URL stored as shown in the picture, but now on my latest run, only url_1 was returned with an actuall URL and the other two was returned as null.

You have only two objects with url_1 value, so what you experience is consistent with your data.

How do I fix that? I can’t see how it has happened. There’s no way of uploading an image without it’s URL being stored.

I’d start with cleaning your data store. Objects without url_1 value either must be removed or fixed using console. Then if it continues to happen, check the code that saves userfilemapping objects…

Splendid, that might have done the trick. I didn’t understand that each row in the table represented a new object. In general, do you have any tips how I can download all my 12 images at once? At the moment I’m sending 12 different HTTP GET requests which looks awful and isn’t very quick.

Thanks for sorting out my issue though. Feel kind of stupid for asking now.

Glad you worked it out. I cannot think of a way to download all images at once. Even web browsers make HTTP requests to load images individually for any given page.