我想以编程方式访问将包含在我的项目文件夹中的特定 Excel 电子表格,并将其上传到 Google 云端硬盘。我将电子表格包含在我的 src 文件夹中并使用以下代码:
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
URL fileURL = getClass().getClassLoader().getResource("Untitled spreadsheet.xlsx");
String filePath2 = fileURL.getPath();
java.io.File fileContent = new java.io.File(filePath2);
FileContent mediaContent = new FileContent("application/vnd.ms-excel", fileContent);
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("application/vnd.ms-excel");
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
if (file != null) {
showToast("File uploaded: " + file.getTitle());
}
else
;
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
但是,我不断收到以下 FileNotFoundException:
11-29 14:43:45.189: W/System.err(21133): java.io.FileNotFoundException: /file:/data/app/com.example.drivequickstart-2.apk!/Untitled spreadsheet.xlsx: open failed: ENOENT (No such file or directory)
有人知道是什么原因造成的吗?
编辑: 我已尝试根据以下建议修改我的代码:
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
String mime = "application/vnd.ms-excel";
InputStream in = getApplicationContext().getAssets().open("Untitled spreadsheet.xlsx");
InputStreamContent content = new InputStreamContent(mime, in);;
File body = new File();
body.setTitle("Untitled spreadsheet");
body.setMimeType("application/vnd.ms-excel");
File file = service.files().insert(body, content).setConvert(true).execute();
if (file != null) {
showToast("File uploaded: " + file.getTitle());
}
else
;
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
但是,这给了我以下错误:
11-29 20:31:08.118: E/AndroidRuntime(9833): java.lang.IllegalArgumentException
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.common.base.Preconditions.checkArgument(Preconditions.java:76)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.api.client.googleapis.media.MediaHttpUploader.getMediaContentLength(MediaHttpUploader.java:328)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:347)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:266)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:408)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:328)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:449)
11-29 20:31:08.118: E/AndroidRuntime(9833): at com.example.drivequickstart.MainActivity$3.run(MainActivity.java:303)
11-29 20:31:08.118: E/AndroidRuntime(9833): at java.lang.Thread.run(Thread.java:864)
并指向该行:File file = service.files().insert(body, content).setConvert(true).execute();
经过仔细检查,我发现长度InputStreamContent 的值为 -1,因此问题可能起源于此。
最佳答案
您使用 Java 惯用法 ClassLoader.getResource()
从 CLASSPATH
加载资源。然而,这不是我们在 Android 上的做法。
您会将文件放在assets/
目录或res/raw/
中,然后使用AssetManager
检索它。或像 R.raw.untitled_spreadsheet.xlsx
这样的标识符。
参见 the official guide :
While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID. Instead, you can save your resources in the assets/ directory.
Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources. Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager.
However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory and read a stream of bytes using openRawResource().
用代码来说,如果将文件放在 assets/sheet.xlsx
中:
String mime = "application/vnd.ms-excel";
InputStream in = ctx.getAssets().open("sheet.xlsx");
InputStreamContent content = new InputStreamContent(mime, in);
其中 ctx
是 Context
.因为 Activity
是一个 Context
你可以跳过 ctx
直接调用 getAssets()
如果你碰巧写了这个Activity
中的代码。
请注意,我没有使用 FileContent
:我选择了 InputStreamContent
,因为您实际上没有 java.io.File
对象,但是 an input stream .
基本上,您是在询问 Drive 客户端:“从此本地流中读取字节,上传它们并使它们可以在名称 $NAME 下访问”。名称 $NAME
通过 com.google.api.services.drive.model.File
对象的 title
字段提供给服务,该字段可以是任何你喜欢的 - 我认为它也可以包括目录分隔符。
这是指向 Javadoc for Google Drive V2 的链接
关于java - 上传到 Google Drive 文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13634434/