save an object with a file reference

I want to save an object with a file reference

In my javaCode i set the object property to the file reference but when i save the file using save api of backendless it alwyas give me an error (Missing ___class property for entity: photo)
How can i save an object and relate a file to it in java code

Hello, Mahmoud.
Firstly, you should save file with Backendless.Files.saveFile( ), .upload() or another one and get it url.
Then you can add this url as property to you object.
After that when you need a file just use url to download it.

Thanks Mr Oleg

I have done so but i have another problem
I upload the file and return back the url but unfortunately the save code executes while the url is still null so i get an error and i done the save code inside runOnUiThread runnable object but it didn’t work too
Do you have any ideas how can i solve this problem
Thanks in Advance

Put the second call (save) into the handleResponse method, so that it is executed only after the response is received.

Hi Mahmoud,

Have you tried something like this:

final File picFile = new File( this.getClass().getResource( "/photo.jpg" ).getFile() );
final byte[] picBytes = Files.readAllBytes( picFile.toPath() );
 Backendless.Files.saveFile( "/", "photo.jpg", picBytes, true, new AsyncCallback<java.lang.String>()
 {
 @Override
 public void handleResponse( final String photoPath )
 {
 Backendless.Data.save( new ObjectWithPhoto( photoPath ), new AsyncCallback() {...} );
 }
 @Override
 public void handleFault( final BackendlessFault fault )
 {
 }
 } );

Regrds,

Denys