Start a new topic

Publicly-readable file upload not working

This is my code:



FileMetaData metadata = new FileMetaData();

metadata.setPublic(true);

metadata.setFileName("picture");

java.io.File file = new java.io.File(getRealPathFromURI(data.getData()));

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

getClient().file().upload(metadata, file, new UploaderProgressListener() {

@Override

public void onSuccess(Void result) {

Log.i("UPLOAD", "successfully upload file");

}

@Override

public void onFailure(Throwable error) {

Log.e("UPLOAD", "failed to upload file.", error);

}

@Override

public void progressChanged(MediaHttpUploader uploader) throws IOException {

Log.i("UPLOADING", "upload progress: " + uploader.getUploadState());

}

});



If I comment out metadata.setPublic(true); the code works fine, ends up in onSuccess() ...but my file is private.



If I un-comment the "setPublic" line, I have an error thrown back:



failed to upload file.

java.lang.IllegalArgumentException: invalid value for field

at java.lang.reflect.Field.setField(Native Method)

at java.lang.reflect.Field.set(Field.java:588)

at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:247)

at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:208)

at com.google.api.client.util.GenericData.put(GenericData.java:103)

at com.kinvey.java.core.MediaHttpUploader.upload(MediaHttpUploader.java:340)

at com.kinvey.java.core.AbstractKinveyClientRequest.executeUnparsed(AbstractKinveyClientRequest.java:334)

at com.kinvey.java.core.AbstractKinveyClientRequest.executeUnparsed(AbstractKinveyClientRequest.java:285)

at com.kinvey.java.core.AbstractKinveyClientRequest.execute(AbstractKinveyClientRequest.java:355)

at com.kinvey.android.AsyncFile$FileUpload.executeAsync(AsyncFile.java:339)

at com.kinvey.android.AsyncFile$FileUpload.executeAsync(AsyncFile.java:288)

at com.kinvey.android.AsyncClientRequest.doInBackground(AsyncClientRequest.java:58)

at android.os.AsyncTask$2.call(AsyncTask.java:287)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

at java.util.concurrent.FutureTask.run(FutureTask.java:137)

at com.kinvey.android.AsyncClientRequest$KinveySerialExecutor$1.run(AsyncClientRequest.java:121)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)

at java.lang.Thread.run(Thread.java:856)



Am I doing anything wrong?



As an alternative, but less clean, option... is there a way I can retrieve the file's ID?

This way I can try calling "blob" once again to set that file public.








Hey,



I just did some investigation into this issue and it looks like a bug in the android library. There is an issue with the headers necessary for the file upload, which results in the above exception.



I am working through the fix now, and will have a release out later this week.



Thanks for reporting this!
This is great news, thank you :) :)

Let me know when it's done, I'll download the new library.



Apart from this, is there actually a way to retrieve the file's ID? I need to use it right after it's been stored. I can do it on the other platforms, but I haven't been able to find a way on Android.
Just a quick update, I am finishing up the fix for this and am planning on releasing a new version of the library this afternoon with the fix.



For this release I am also introducing two new classes: `MetaDownloadProgressListener` and `MetaUploadProgressListener` -- you can use this as your base class for upload or download callbacks, and they both provide a new method `getMetadata()`, which you can call in `onSuccess` and it will return the metadata. From this metadata object you can grab the id of the recently uploaded (or downloaded) file.



Let me know if that'll work for ya!
Wow, thanks! :) Okay I'll test it asap and I'll let you know
Hey I just released version 2.6.12 of the android library, which has a fix for this issue!



http://devcenter.kinvey.com/android/downloads



Thanks again for reporting it, and let me know if you find anything else or have any feature requests for the future!
Okay I tried it now, uploads public files well, and the new getMetadata too ;)



for whoever needs to do something similar, this is now how the function should look like:



getClient().file().upload(metadata, file, new MetaUploadProgressListener() {

@Override

public void onSuccess(Void result) {

Log.i("UPLOAD", "successfully upload file");

FileMetaData fmd = getMetadata();

String thumbID = fmd.getId();

...

Hey,

I've used the same piece of code, upload is successful but the image file uploaded turns out to be invalid.

What might be the reason?


How do I download it?

Please help its very urgent!

Nidhi:


I'm not entirely sure what you mean by this?  Downloading files via the datastore is covered in depth in our guides (Under the heading "fetching".    For example:  http://devcenter.kinvey.com/html5/guides/datastore#Fetching ).


If you can be more specific about what you've tried and what issues you are running into I'm sure this is something we can get figured out.  Can you reply with more information including code that you've tried?


Thanks,

This is the Upload code:

FileMetaData metadata = new FileMetaData();

      metadata.setPublic(true);

      metadata.setFileName(file.getName());

      kinveyClient.file().upload(metadata,file, new MetaUploadProgressListener() {

   

   public void onSuccess(Void arg0) {

    // TODO Auto-generated method stub

    Log.i("TAG", "successfully upload bytes");

              s="suc";

             

              list.add(getMetadata().getId());

   }

   

   public void onFailure(Throwable error) {

    // TODO Auto-generated method stub

    Log.e("TAG", "failed to upload bytes.", error);

              s="fail";

   }

   

   public void progressChanged(MediaHttpUploader uploader) throws IOException {

    // TODO Auto-generated method stub

    Log.i("TAG", "upload progress: " + uploader.getUploadState());

              // all updates to UI widgets need to be done on the UI thread

    s="pro";

   }

      });



download code:

File file=new File(nam);

     try {

   file.createNewFile();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

     

  FileMetaData meta=new FileMetaData(nam);

     FileOutputStream fos=null;

  try {

   fos = new FileOutputStream(file);

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

     kinveyClient.file().download(meta, fos, new DownloaderProgressListener() {

         public void onSuccess(Void result) {

             Log.i("TAG", "successfully downloaded file");

             text1.setText("s");

         }

         public void onFailure(Throwable error) {

             Log.e("TAG", "failed to downloaded file.", error);

             text1.setText("f");

         }

         public void progressChanged(MediaHttpDownloader downloader) throws IOException {

             Log.i("TAG", "progress updated: "+downloader.getDownloadState());

             // any updates to UI widgets must be done on the UI thread

             text1.setText("pro");

         }

     });

     

Download is not working at all. And even the uploaded files are invalid. I'm not able to figure out the reason.

And the platform is Android.

I'm working on Android.

This is the upload code:

FileMetaData metadata = new FileMetaData();

      metadata.setPublic(true);

      metadata.setFileName(file.getName());

      kinveyClient.file().upload(metadata,file, new MetaUploadProgressListener() {

   

   public void onSuccess(Void arg0) {

    // TODO Auto-generated method stub

    Log.i("TAG", "successfully upload bytes");

              s="suc";

             

              list.add(getMetadata().getId());

   }

   

   public void onFailure(Throwable error) {

    // TODO Auto-generated method stub

    Log.e("TAG", "failed to upload bytes.", error);

              s="fail";

   }

   

   public void progressChanged(MediaHttpUploader uploader) throws IOException {

    // TODO Auto-generated method stub

    Log.i("TAG", "upload progress: " + uploader.getUploadState());

              // all updates to UI widgets need to be done on the UI thread

    s="pro";

   }

      });


This is the download code:

File file=new File(nam);

     try {

   file.createNewFile();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

     

  FileMetaData meta=new FileMetaData(nam);

     FileOutputStream fos=null;

  try {

   fos = new FileOutputStream(file);

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

     kinveyClient.file().download(meta, fos, new DownloaderProgressListener() {

         public void onSuccess(Void result) {

             Log.i("TAG", "successfully downloaded file");

             text1.setText("s");

         }

         public void onFailure(Throwable error) {

             Log.e("TAG", "failed to downloaded file.", error);

             text1.setText("f");

         }

         public void progressChanged(MediaHttpDownloader downloader) throws IOException {

             Log.i("TAG", "progress updated: "+downloader.getDownloadState());

             // any updates to UI widgets must be done on the UI thread

             text1.setText("pro");

         }

     });

     


The files are not downloading, and even the ones uploaded are invalid files (images). Not able to figure out the reason. It would be great, if you could help.

Nidhi:  


Did you check out the security guide for "globally readable" files?    http://devcenter.kinvey.com/android/guides/security#GlobalAccessControl   I think that's likely what you are after.


Thanks,

But I'm not working with entities!

Nidhi:


Files are treated as entities within Kinvey.


Can you attempt to manually download one of the files you upload and ensure it's in the format you expect it to be in?   Would you be comfortable sharing the code that you're using to create / get the file for uploading?   Specifically where you define the file used in the following line of code:


 kinveyClient.file().upload(metadata,file, new MetaUploadProgressListener() {


Thanks,

Login or Signup to post a comment