2014-03-28 35 views
0

我想读取我写在Appfolder中的文件上,但我无法读取我的应用程序崩溃时,我尝试从file.i中读取该文件successfully.i现在用下面的代码,所以当我运行这段代码,请告诉我们,如果我在做这来了什么wrong.The错误的应用程序文件夹无效驱动器id.i我得到该驱动器ID本:在Android中读取AppFolder文件数据

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

} 

回答

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

我还应该注意,通过从'run(){}'中删除它并用async'content result callback'替换'await()'方法,可以轻松地将代码转换为异步版本。 – seanpj

2

后寻找一个很好的例子的日子里,我找到了Google I/O app on GitHub具有良好的实用方法来创建,更新和读取文件来自Drive。他们也使用AppFolder。