我想阅读我已经在Appfolder中写的文件。但是我无法读取我的应用程序崩溃当我尝试从file.i已经成功地在App文件夹中创建了该文件。我是使用下面的代码,所以请告诉我,如果我做错了什么。我运行此代码时出现的错误是无效的驱动器ID.i正在通过此获取该驱动器ID:
result.getDriveFile().getDriveId().encodeToString()
结果是drivefileresult。我可以知道实现目标的正确方法是什么?
public class Fifth extends BaseDemoActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
}
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
// create new contents resource
Drive.DriveApi.newContents(getGoogleApiClient())
.setResultCallback(contentsCallback);
}
final private ResultCallback<ContentsResult> contentsCallback = new
ResultCallback<ContentsResult>() {
@Override
public void onResult(ContentsResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create new file contents");
return;
}
// Get an output stream for the contents.
OutputStream outputStream = result.getContents().getOutputStream();
// Write the bitmap data from it.
String data="hello world. this is sample";
byte[] bytes = data.getBytes();
// ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
// image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
Log.i("Success", "able to write file contents.");
outputStream.write(bytes);
} catch (IOException e1) {
Log.i("Failier", "Unable to write file contents.");
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("appdatafolder.txt")
.setMimeType("text/plain")
.build();
Drive.DriveApi.getAppFolder(getGoogleApiClient())
.createFile(getGoogleApiClient(), changeSet, result.getContents())
.setResultCallback(fileCallback);
}
};
final private ResultCallback<DriveFileResult> fileCallback = new
ResultCallback<DriveFileResult>() {
@Override
public void onResult(DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the file");
return;
}
showMessage("Created a file in App Folder: "
+ result.getDriveFile().getDriveId());
Log.i("Drioved_ID", result.getDriveFile().getDriveId().encodeToString());
}
};
}
答案 0 :(得分:3)
不幸的是,我没有时间分析您的代码,但可以提供基本相同的细分。试试吧。
GoogleApiClient gac = getGoogleApiClient();
DriveFolder dfl = Drive.DriveApi.getAppFolder(gac)
String title = "appdatafolder.txt";
String mime = "text/plain";
byte[] buff = ("hello world. this is sample").getBytes();
createFile(gac, dfl, title, mime, buff);
void createFile(final GoogleApiClient gac, final DriveFolder fldr,
final String name, final String mime, final byte[] buff) {
Thread t = new Thread(new Runnable() {
@Override public void run() {
try {
ContentsResult rslt = Drive.DriveApi.newContents(gac).await();
if (rslt.getStatus().isSuccess()) {
Contents cont = rslt.getContents();
cont.getOutputStream().write(buff);
MetadataChangeSet meta =
new MetadataChangeSet.Builder().setTitle(name).setMimeType(mime).build();
DriveFile df = fldr.createFile(gac, meta, cont).await().getDriveFile();
Log.i("X", ""+ df.getDriveId().encodeToString());
}
} catch (Exception e) {}
}
});
t.start();
}
......以下是您回读的方式:
void getFileIs(final GoogleApiClient gac, final DriveId drvId) {
Thread t = new Thread(new Runnable() {
@Override public void run() {
try {
DriveFile df = Drive.DriveApi.getFile(gac, drvId);
ContentsResult rslt = df.openContents(gac, DriveFile.MODE_READ_ONLY, null).await();
if (rslt.getStatus().isSuccess()){
InputStream is = rslt.getContents().getInputStream();
}
} catch (Exception e) {}
}
});
t.start();
}
答案 1 :(得分:2)
经过几天寻找一个好例子后,我发现Google I/O app on GitHub具有很好的实用工具方法,可以从云端硬盘创建,更新和读取文件。他们也使用AppFolder。