Current Android download on Google Drive

I am testing the Drive API for Android to upload a file that can show downloaded progress and be able to resume downloading if it does not work (file size> 30 MB.)

With the following questions: Download Uploading a large file to Google Drive, resulting in an error , Do not show downloadable listener (Google Drive API) I was able to get the download progress, and they mention that these are renewable downloads. However, I don’t see any code that looks for a loading error and resumes the logic, so if I kill the application and “resume” the download, it just starts from the beginning.

This is my code:

public class DriveScreen extends BaseDriveActivity { private Drive service; private Context context; @Override public void onConnected(Bundle connectionHint) { super.onConnected(connectionHint); context = this; //https://stackoverflow.com/questions/17429798/usingoauth2-deprecated GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE)); credential.setSelectedAccountName("accountNameHERE"); service = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build(); UploadFile(); } public void UploadFile() { AsyncTask<Void, Long, String> task = new AsyncTask<Void, Long, String>() { java.io.File fileContent; FileContent mediaContent; com.google.api.services.drive.model.File body; com.google.api.services.drive.model.File file; private ProgressDialog mDialog; long mFileLen; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); mDialog = new ProgressDialog(context); mDialog.setMax(100); mDialog.setMessage("Uploading "); mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mDialog.setProgress(0); mDialog.setButton("Cancel", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); mDialog.show(); } class FileUploadProgressListener implements MediaHttpUploaderProgressListener { @Override public void progressChanged(MediaHttpUploader uploader) throws IOException { Log.d("Percent ", String.valueOf(uploader.getProgress())); switch (uploader.getUploadState()) { case INITIATION_STARTED: System.out.println("Initiation Started"); break; case INITIATION_COMPLETE: System.out.println("Initiation Completed"); break; case MEDIA_IN_PROGRESS: System.out.println("Upload in progress"); System.out.println("Upload percentage: " + uploader.getProgress()); mDialog.setProgress((int) (uploader.getProgress()*100)); break; case MEDIA_COMPLETE: System.out.println("Upload Completed!"); break; case NOT_STARTED: System.out.println("Upload Not Started!"); break; } } } @Override protected String doInBackground(Void... arg0) { try { File folder = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ); File UPLOAD_FILE = new File(folder, "filePathHERE"); fileContent = new File(folder, "filePathHERE"); mFileLen = fileContent.length(); InputStreamContent mediaContent2 = new InputStreamContent("application/zip", new FileInputStream(UPLOAD_FILE)); mediaContent2.setLength(UPLOAD_FILE.length()); body = new com.google.api.services.drive.model.File(); body.setTitle(fileContent.getName()); body.setMimeType("application/zip"); //String parentId = null; //int x = Files.List.setQ("mimeType = 'application/vnd.google-apps.folder' and title = 'ShareHim'"); //body.setParents(Arrays.asList(new ParentReference().setId(uploadFile.getFileHostFolderId()))); Files.Insert mInsert = service.files().insert(body, mediaContent2); if(mFileLen > 5 * 1024 * 1024) { MediaHttpUploader uploader = mInsert.getMediaHttpUploader(); uploader.setDirectUploadEnabled(false); uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE); uploader.setProgressListener(new FileUploadProgressListener()); file = mInsert.execute(); if (file != null) { } } else { mInsert.execute(); } } catch (UserRecoverableAuthIOException e) { System.out.println("login error"); Log.d("Error", "not login " + e); } catch (IOException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String result) { mDialog.dismiss(); }; }; task.execute(); } } 

Now it loads in pieces, but how can we resume the download from the last block sent. My first impression after watching conversations on Google Drive on YouTube was that the renewable download was handled automatically, but it doesn't seem to be that way.

Am I approaching this wrong? Should I consider using DriveSyncAdapter to download a file as an alternative? (Sorry for the bad code, this is just a test)

0
source share
1 answer

It looks like you are using the Java REST-ful API. If you use the Android-specific API, all of this will be done for you. Just transfer the file and the API will reload as needed. See creating files.

0
source

Source: https://habr.com/ru/post/1257762/


All Articles