简体   繁体   English

如何在Android中从Kinvey下载文件

[英]How to download file from kinvey in android

Trying to download publicly accessible file from kinvey to android app tried following code, 尝试从kinvey将可公开访问的文件下载到android应用程序尝试以下代码,

FileOutputStream fos = new FileOutputStream(videoName);
        FileMetaData meta = new FileMetaData(videoName);
        meta.setId(videoName);
        kinveyClient.file().downloadWithTTL(meta.getId(), 1200000, fos, new DownloaderProgressListener() {
            @Override
            public void progressChanged(MediaHttpDownloader downloader) throws IOException {
                Log.i("", "progress updated: " + downloader.getDownloadState());
                final String state = new String(downloader.getDownloadState().toString());

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //tProgress.setText((tProgress.getText() == null) ? state : tProgress.getText() + "\n" + state)
                    }
                });
            }

            @Override
            public void onSuccess(Void result) {
            }

            @Override
            public void onFailure(Throwable error) {
            }
        });

getting event success but data is not being received. 获得事件成功,但未接收到数据。 any idea how this will work?? 任何想法这将如何工作?

getting error like this : 得到这样的错误:

failure com.kinvey.java.core.KinveyJsonResponseException: InsufficientCredentials
                                                          The credentials used to authenticate this request are not authorized to run this operation. Please retry your request with appropriate credentials

According to the documentation, this is how you are supposed to use the value you get from getDownloadState() . 根据文档,这就是应该使用从getDownloadState()获得的值的方式。 In order to get the progress of your download, you need to first check the downloadState is equal to DOWNLOAD_IN_PROGRESS value, and then use the download.getProgress() method to get the progress, then you can use this to update the value on your progress slider, (you shouldn't be converting these values to string using the toString() method like you've done above. 为了获得下载进度,您需要首先检查downloadState等于DOWNLOAD_IN_PROGRESS值,然后使用download.getProgress()方法获取进度,然后可以使用它来更新进度值滑块,(您不应像上面那样使用toString()方法将这些值转换为字符串。

public void progressChanged(MediaHttpDownloader downloader) throws IOException {
  switch (downloader.getDownloadState()) {
    case DOWNLOAD_IN_PROGRESS:
      //Download in progress
      //Download percentage:  + downloader.getProgress()
      break;
    case DOWNLOAD_COMPLETE:
      //Download Completed!
      break;
  }
}

Code taken from documentation here 代码来自此处的文档

Nayana, 娜亚娜

You might be getting this error because user does not have permission to access that file. 您可能会收到此错误,因为用户没有访问该文件的权限。 You can try following to solve your issue: 您可以尝试以下解决您的问题:

  • If you make the file publicly readable, all authenticated users will be able to access that particular file. 如果您使文件公开可读,则所有通过身份验证的用户都将能够访问该特定文件。
  • You can make the file accessible by adding read/write permissions for few users in ACL. 您可以通过为ACL中的几个用户添加读写权限来使文件可访问。

Also check the documentation for Access Control List: http://devcenter.kinvey.com/rest/guides/security#entityanduserpermissions 另请参阅访问控制列表的文档: http : //devcenter.kinvey.com/rest/guides/security#entityanduserpermissions

Thanks, Pranav Kinvey Support 谢谢,Pranav Kinvey支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM