Android Google Drive Resumable Upload fails very often

六眼飞鱼酱① 提交于 2019-12-10 18:38:33

问题


I have problem with uploading mp4 files to google drive. I am using resumable upload. Chunk size is set as multiplicity of minimal size(depending on file size to be uploaded in order to make progress bar working). Upload of small files is working almost always. But when file is about 50 MB upload fails very often. There is exception EOFException at the very beginning of upload or exception "Unexpected end of stream" somewhere in the middle of upload. After many tests it looks that behaviour is random, sometimes working, sometimes not. I am using latest Google Drive API downloaded with eclipse plugin. I am using default authorization with "AccountPicker" activity. I have seen many hints on google search but nothing has solved the problem. For example setting "System.setProperty("http.keepAlive", "false")". This is my upload method:

try{
    final java.io.File fileContent = new java.io.File(path);
    // get MIME type from name:
    String mime = getMimeType(fileContent.getAbsolutePath());
    FileContent mediaContent = new FileContent(mime, fileContent);

    // File's meta-data.
    File body = new File();
    body.setTitle(fileContent.getName());
    body.setMimeType(mime);
    // set parent folder:
    body.setParents(Arrays.asList(new ParentReference().setId(parentId)));

    Files f = mService.files();
    Insert i = f.insert(body, mediaContent);
    // configure MediaUploader:
    MediaHttpUploader uploader = i.getMediaHttpUploader();
    uploader.setDirectUploadEnabled(false);
    uploader.setProgressListener(new FileUploadProgressListener());
    int chunkSize = getDataChunkSize(fileContent.length());
    uploader.setChunkSize(chunkSize);
    File file = i.execute();
    // upload was interrupted
    if (Thread.interrupted() == true) {
        // file upload was interrupted, currently no action here.
    }
    else if (file != null) {
        // upload finished with success.
    }
    else if (file == null) {
        // upload failed.
    }
}
catch (Exception e) {
    // upload failed:
}

I noticed that "EOFException" is generated mainly when google drive is not used for longer time and upload request is done. When for example listing of folders is done and immediately after it upload is done "EOFException" is not generated. I am creating credential object and drive service object only once per activity time life. Maybe some refresh is needed? Could you please help with this? Thanks in advance.

来源:https://stackoverflow.com/questions/19349405/android-google-drive-resumable-upload-fails-very-often

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!