0

我想阅读我在 Appfolder 中的文件上写入的内容。但是当我尝试从文件中读取该文件时,我无法读取我的应用程序崩溃。我已成功在 App 文件夹中创建了该文件。我正在使用以下内容代码所以请告诉我是否做错了什么。运行此代码时出现的错误是无效的驱动器ID。我通过以下方式获取该驱动器ID:

result.getDriveFile().getDriveId().encodeToString()

其中结果是驱动文件结果。我可以知道实现目标的正确方法是什么吗?

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());
            }
        };

}
4

2 回答 2

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();
}
于 2014-03-30T02:01:10.193 回答
2

经过几天的搜索,我发现GitHub 上的 Google I/O 应用程序具有出色的实用方法来创建、更新和读取 Drive 中的文件。他们也使用 AppFolder。

于 2016-03-15T12:56:55.637 回答