0
我想上傳一個空的臨時文本文件到谷歌驅動器,但它沒有成功。 file
返回null。不過,我可以使用谷歌驅動器網站上的示例從我的相機上傳圖片。我是否需要爲緩存設置任何特殊權限?謝謝你的幫助。Android:上傳文本文件到谷歌驅動器
這是我的嘗試:
java.io.File outputDir = getApplicationContext().getCacheDir(); // context being the Activity pointer
java.io.File tempFile = java.io.File.createTempFile("hello", "txt", outputDir);
FileContent mediaContent = new FileContent("text/plain", tempFile);
// File's metadata.
File body = new File();
body.setTitle(tempFile.getName());
body.setMimeType("text/plain");
File file = service.files().insert(body, mediaContent).execute();
谷歌如何例子做的:
String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_"
+ timeStamp + ".jpg"));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
saveFileToDrive();
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
}
});}
你確定'body'和'mediaContent'都是null不同? –
你想把image/jpeg文件存儲到lain文本文件中,所以我認爲你需要再次檢查你的代碼,並且在執行一些步驟後嘗試記錄你的輸出 –
@NaNNy對不起,我不確定你是什麼意思?你是說他們可能是空的?我檢查過,他們不爲空 – roverred