2
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SELECT_PICTURE) {
        if (resultCode == RESULT_OK) {
            Bitmap bitmap = null;
            if (data != null) {
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                userPicImageView.setImageBitmap(bitmap);
                dialog = new ProgressDialog(this);
                dialog.setTitle("Updating profile picture");
                dialog.setMessage("Please, wait");
                dialog.show();

                final BackendlessUser user = Backendless.UserService.CurrentUser();
                Backendless.Files.Android.upload(bitmap, Bitmap.CompressFormat.JPEG, 100, "profile_pic_" + user.getObjectId() + System.currentTimeMillis(), "images", new AsyncCallback<BackendlessFile>() {
                    @Override
                    public void handleResponse(final BackendlessFile response) {
                        Log.d(TAG, "updated profile pic");
                        Log.d(TAG,"FILE URL IS "+response.getFileURL().toString());
                        backendlessFile = response;
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                        String currentUserID = Backendless.UserService.loggedInUser();
                        Backendless.UserService.findById(currentUserID, new AsyncCallback<BackendlessUser>() {
                            @Override
                            public void handleResponse(BackendlessUser usr) {
                                Log.d(TAG,"CURRENT USER is "+usr.getProperty("name").toString());
                                usr.setProperty("image", response.getFileURL().toString());
                                Backendless.UserService.update(usr);
                            }
                            @Override
                            public void handleFault(BackendlessFault fault) {
                                [**problem here**-->] Log.e(TAG,"COULD NOT LOAD user");
                            }
                        });
                    }
                    @Override
                    public void handleFault(BackendlessFault fault) {
                        Log.e(TAG, "error " + fault.getMessage());
                    }
                });
            }
        }
    }
}

The above code handles the case when a user updates her profile with a picture from gallery. She uploads the file all right to Backendless file storage. The Users table has a column "image" that stores a link to the profile picture. I try to retrieve the current user and update her row by setting the image field to contain the link to the uploaded file, but the logs say they cannot access the user (the error location is marked problem here in the above code). Does this all have to do with Async requests? How can this be fixed?

[EDIT] I figured out that it all has to do with Async calls indeed. The thing is, Backenledss AsyncCallback seems not to have onPostExecute() method.

Ilonpilaaja
  • 1,169
  • 2
  • 15
  • 26
  • Why are you using logged in user instead of current user? – Wain Jun 18 '16 at 10:10
  • @Wain: I used current user as well, but it didn't work either. When I print out the Logs, it appears that the program tries to update user's "image" field prior to the upload, hence the problem. – Ilonpilaaja Jun 18 '16 at 10:37
  • @ilonpilaaja, are you saying the "handleResponse" for Backendless.Files.Android.upload is called before the upload actually happens? I think this is not possible, since handleResponse brings you back the file url for the file you uploaded. – Mark Piller Jun 18 '16 at 11:46
  • @MarkPiller, I've opened a topic on Backendless support forum, "onPostExecute() for AsyncCallback" – Ilonpilaaja Jun 18 '16 at 12:17

0 Answers0