我想使用新的Android云端硬盘API以编程方式将大文件上传到Google云端硬盘.您能为我推荐一个具有最佳实践的教程吗?
我正在读取此post on stack overflow,但在那里使用缓冲区将它们加载到内存中.
解决方法:
我一直以quick start from Google为起点.它显示了如何上传位图,但很容易适应.他们还会将整个内容保存为字节数组,然后将其写入Google云端硬盘的输出流.如果您不想执行此 *** 作,那么我建议您执行以下 *** 作:
/** * Create a new file and save it to Drive. */ private voID savefileToDrive(final file file) { // Start by creating a new contents, and setting a callback. Log.i(TAG, "Creating new contents."); Drive.DriveAPI.newDriveContents(mGoogleapiclient) .setResultCallback(new ResultCallback<DriveAPI.DriveContentsResult>() { @OverrIDe public voID onResult(DriveAPI.DriveContentsResult result) { // If the operation was not successful, we cannot do anything // and must // fail. if (!result.getStatus().isSuccess()) { Log.i(TAG, "Failed to create new contents."); return; } // Otherwise, we can write our data to the new contents. Log.i(TAG, "New contents created."); // Get an output stream for the contents. OutputStream outputStream = result.getDriveContents().getoutputStream(); // Write the bitmap data from it. try { fileinputStream fileinputStream = new fileinputStream(file); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fileinputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e1) { Log.i(TAG, "Unable to write file contents."); } // Create the initial Metadata - MIME type and Title. // Note that the user will be able to change the Title later. MetadataChangeSet MetadataChangeSet = new MetadataChangeSet.Builder() .setTitle(file.getname()).build(); // Create an intent for the file chooser, and start it. IntentSender intentSender = Drive.DriveAPI .newCreatefileActivityBuilder() .setinitialMetadata(MetadataChangeSet) .setinitialDriveContents(result.getDriveContents()) .build(mGoogleapiclient); try { startIntentSenderForResult( intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { Log.i(TAG, "Failed to launch file chooser."); } } }); }
通过快速入门中的设置,您可以使用此方法上传文件.
总结以上是内存溢出为你收集整理的android-使用GD API将大文件上传到Google云端硬盘全部内容,希望文章能够帮你解决android-使用GD API将大文件上传到Google云端硬盘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)