上传文件后获取谷歌硬盘的URL-Android
在我的应用程序中,我需要拍摄照片并管理其存储。我有两个选择,将照片保存在本地内存或Google Drive中,并且我必须跟踪SQLite数据库中的任何照片。我的应用程序运行得很好,但我有一个大问题: 如果我决定将照片保存在Google Drive中,当我在Google Drive的“活动结果”中按OK(在此处选择文件名和驱动器文件夹)时,我必须获取我刚刚上传到Google Drive帐户中的文件的URL。 我该怎么做?我想我必须在activityresult方法中执行一些操作 我留下了我当前的应用程序代码,非常好用! 备注:位图上传文件后获取谷歌硬盘的URL-Android,android,url,google-drive-api,android-bitmap,google-drive-android-api,Android,Url,Google Drive Api,Android Bitmap,Google Drive Android Api,在我的应用程序中,我需要拍摄照片并管理其存储。我有两个选择,将照片保存在本地内存或Google Drive中,并且我必须跟踪SQLite数据库中的任何照片。我的应用程序运行得很好,但我有一个大问题: 如果我决定将照片保存在Google Drive中,当我在Google Drive的“活动结果”中按OK(在此处选择文件名和驱动器文件夹)时,我必须获取我刚刚上传到Google Drive帐户中的文件的URL。 我该怎么做?我想我必须在activityresult方法中执行一些操作 我留下了我当前的应
newPhoto
包含相机拍摄的照片;)
谢谢你的回答
private void saveFileToDrive() {
//Start creating new content and setting a callback
Log.i(TAG, "Creating new Content");
final Bitmap image = newPhoto;
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(@NonNull DriveContentsResult result) {
//if operation was not successful we cannot do anything
//and ust fail
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Fail to create new content");
return;
}
// otherwise, we can write our data to the new content
Log.i(TAG, "New content created");
//creation of an outputstream for the new content
OutputStream outputStream = result.getDriveContents().getOutputStream();
//Write the bitmap data from it
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents");
}
// Create the initial metadata (Type non title)
// NB: user is able to change the title
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("image/jpeg")
.setTitle(imageFileName + ".jpg").build();
// Create an intent for the file chooser
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(intentSender, DRIVE_REQUEST, null, 0, 0, 0);
} catch (SendIntentException e) {
Log.i(TAG, "Fail to lauch file chooser");
}
}
});
}
private void saveFileToDrive(){
//开始创建新内容并设置回调
Log.i(标记“创建新内容”);
最终位图图像=新照片;
Drive.DriveApi.newDriveContents(mgoogleapClient)
.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(@NonNull DriveContentsResult result){
//如果手术不成功,我们什么也做不了
//我们必须失败
如果(!result.getStatus().issucess()){
Log.i(标记“无法创建新内容”);
返回;
}
//否则,我们可以将数据写入新内容
Log.i(标记“创建的新内容”);
//为新内容创建输出流
OutputStream OutputStream=result.getDriveContents().getOutputStream();
//从中写入位图数据
ByteArrayOutputStream bitmapStream=新建ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG,100,bitmapStream);
试一试{
write(bitmapStream.toByteArray());
}捕获(IOE1异常){
Log.i(标记“无法写入文件内容”);
}
//创建初始元数据(键入非标题)
//注意:用户可以更改标题
MetadataChangeSet MetadataChangeSet=新建MetadataChangeSet.Builder()
.setMimeType(“图像/jpeg”)
.setTitle(imageFileName+“.jpg”).build();
//为文件选择器创建意图
IntentSender IntentSender=Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(结果.getDriveContents())
.构建(mGoogleApiClient);
试一试{
startinentsenderforresult(intentSender,驱动器请求,null,0,0);
}捕获(发送){
Log.i(标记“启动文件选择器失败”);
}
}
});
}
image.compress(Bitmap.CompressFormat.PNG,100,bitmapStream)代码>。更改为image.compress(Bitmap.CompressFormat.PNG,100,outputStream)
并删除字节数组输出流。//为文件选择器创建意图。我不明白。您刚刚将内容保存到google drive。你为什么现在选择一个文件?我同意你的第一个答案。感谢第二个,我想让用户有可能改变文件名,并选择一个不同的文件夹上传图像。不知道如何获取url?