使用 intent 将文件上传到 Google 云端硬盘
Use intent to upload files to Google Drive
我收到错误:"Upload failed. Failed to schedule 1 file for upload"。我正在尝试创建一个简单的文件资源管理器。当用户点击 listView 上的非文件夹项目时,将出现 Google 驱动器上传对话框,但当我从 Google 驱动器对话框中点击保存按钮时出现错误。下面显示了我的编码。请告诉我我做错了什么或哪些部分。谢谢。
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
currentDir = new File(o.getPath());
fill(currentDir);
}
else
{
onFileClick(o);
}
}
private void onFileClick(Option o)
{
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
String uri = o.getPath();
Uri uploadUri = Uri.parse(uri);
Toast.makeText(this, "This file is located at: "+uploadUri, Toast.LENGTH_SHORT).show();
if (uri.contains(".txt")) {
Intent uploadIntent = ShareCompat.IntentBuilder.from(this)
.setText("Share Document")
.setType("application/txt")
.setStream(uploadUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(uploadIntent);
}else{
//TODO: what to do when it is other file format
}
}
在 onFileClick() 内部:
改变
String uri = o.getPath();
至
String uri = "file://" + o.getPath();
好了! :)
我收到错误:"Upload failed. Failed to schedule 1 file for upload"。我正在尝试创建一个简单的文件资源管理器。当用户点击 listView 上的非文件夹项目时,将出现 Google 驱动器上传对话框,但当我从 Google 驱动器对话框中点击保存按钮时出现错误。下面显示了我的编码。请告诉我我做错了什么或哪些部分。谢谢。
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
currentDir = new File(o.getPath());
fill(currentDir);
}
else
{
onFileClick(o);
}
}
private void onFileClick(Option o)
{
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
String uri = o.getPath();
Uri uploadUri = Uri.parse(uri);
Toast.makeText(this, "This file is located at: "+uploadUri, Toast.LENGTH_SHORT).show();
if (uri.contains(".txt")) {
Intent uploadIntent = ShareCompat.IntentBuilder.from(this)
.setText("Share Document")
.setType("application/txt")
.setStream(uploadUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(uploadIntent);
}else{
//TODO: what to do when it is other file format
}
}
在 onFileClick() 内部:
改变
String uri = o.getPath();
至
String uri = "file://" + o.getPath();
好了! :)