javaandroidandroid-studiokinveycloud-storage

Trying to upload an image from android app to cloud using kinvey


I'm trying to upload an image, saved in the internal memory of my phone, clicked by an android app to a cloud service, and am using Kinvey to do so. But I'm facing some problems with it.

Every time I run the code that contains the upload part, I encounter an exception. I'm uploading an image of ".png" type. Any conversion to blob is not a necessary process that is required, unlike that in Google Cloud Platform.

Here is my .java code -

`Client mKinveyClient = new Client.Builder(APP_KEY, SECRET_KEY, this.getApplicationContext()).build();
    mKinveyClient.enableDebugLogging();

    mKinveyClient.ping(new KinveyPingCallback() {
        public void onFailure(Throwable t) {
            Log.e(TAG, "Kinvey Ping Failed", t);
        }
        public void onSuccess(Boolean b) {

            Log.d(TAG, "Kinvey Ping Success");
        }
    });

    java.io.File file = new java.io.File(Environment.getExternalStorageDirectory().getPath() + "/Camera/" + "IMG_20161115_193353.jpg");

    mKinveyClient.file().upload(file, new UploaderProgressListener() {
            public void onSuccess(Void result) {
            Log.i(TAG, "successfully upload file");
        }

        @Override
        public void onSuccess(FileMetaData fileMetaData) { 
                Log.i(TAG, "successfully uploaded file");
            }
        @Override
        public void onFailure(Throwable error) {
            Log.e(TAG, "failed to upload file.", error);
            }
        @Override
        public void progressChanged(MediaHttpUploader uploader) throws IOException {
            Log.i(TAG, "upload progress: " + uploader.getUploadState());                       // all updates to UI widgets need to be done on the UI thread
            }
        });`

Now, although the ping call is giving me a successful response, the upload part is giving me an error.

E/Activity file: failed to upload file. com.kinvey.java.KinveyException: REASON: No user is currently logged in.

I've searched a lot on this topic, on kinvey's discussion platform and here too. But I'm still stuck. I don't know where I'm going wrong or what I might be missing.

If anyone is out there who has successfully managed to upload images through kinvey then please help me out.


Solution

  • Any kind of Kinvey operations must be provided via a logged user. You must sign up (or login) before you start to i/o any information.

    mKinveyClient.user().create("username", "password", new KinveyUserCallback()) {
        @Override
        public void onFailure(Throwable t) {
            CharSequence text = "Could not sign up.";
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onSuccess(User u) {
            //here we go on uploading file
        }
    });
    

    You can find more info about users here