2016-05-25 90 views
0

使用此代碼,我可以獲取位於Google雲端硬盤根目錄中的文件的內容。現在我的要求是將byte []數組轉換爲文件並將其保存在外部存儲設備中。調試時,我得到這個錯誤:Android將字節轉換爲文件

at libcore.io.IoBridge.open(IoBridge.java:456) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at java.io.FileInputStream.<init>(FileInputStream.java:76) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at com.spjanson.gdaademo.MainActivity$4.doInBackground(MainActivity.java:276) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at com.spjanson.gdaademo.MainActivity$4.doInBackground(MainActivity.java:250) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at java.lang.Thread.run(Thread.java:818) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err: Caused by: android.system.ErrnoException: open failed: ENAMETOOLONG (File name too long) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at libcore.io.Posix.open(Native Method) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:442) 
05-25 18:08:56.787 19204-20008/com.spjanson.gdaademo W/System.err: ... 9 more 

靜態方法來從驅動器獲取文件

static byte[] read(String id) { 
    byte[] buf = null; 
    if (mGAC != null && mGAC.isConnected() && id != null) try { 
     DriveFile df = Drive.DriveApi.getFile(mGAC, DriveId.decodeFromString(id)); 
     DriveContentsResult rslt = df.open(mGAC, DriveFile.MODE_READ_ONLY, null).await(); 
     if ((rslt != null) && rslt.getStatus().isSuccess()) { 
      DriveContents cont = rslt.getDriveContents(); 
      buf = UT.is2Bytes(cont.getInputStream()); 
      cont.discard(mGAC); // or cont.commit(); they are equiv if READONLY 
     } 
    } catch (Exception e) { 
     UT.le(e); 
    } 
    return buf; 
} 

異步保存文件

@Override 
      protected Void doInBackground(Void... params) { 
       mBusy = true; 
       ArrayList<ContentValues> cvs = GDAA.searchDB(UT.FILE_NAME); 
       if (cvs != null) for (ContentValues cv : cvs) { 
        String gdid = cv.getAsString(UT.GDID); 
        System.out.println("ID..... " + gdid); 
        byte[] buf = GDAA.read(gdid); 

        String str = null; 
        if (buf != null) { 
         try { 
          str = new String(buf, "UTF-8"); 
         } catch (UnsupportedEncodingException e) { 
          e.printStackTrace(); 
         } 

         FileChannel source = null; 
         FileChannel destination = null; 
         File sd = new File(Environment.getExternalStorageDirectory(), "myfile.db"); 
         String backupDBPath = "myfile.db"; 
         File internalDB = new File(str); 
         File backupDB = new File(sd, backupDBPath); 
         try { 
          source = new FileInputStream(internalDB).getChannel(); 
          destination = new FileOutputStream(backupDB).getChannel(); 
          destination.transferFrom(source, 0, source.size()); 
          source.close(); 
          destination.close(); 

         } catch (IOException e) { 
          e.printStackTrace(); 

         } 

         //String str = buf == null ? "" : new String(buf); 
         //File fl = UT.str2File(str, "database.db"); 
         } 


       } 

       return null; 
      } 
+0

要將其保存爲** yourFile.bin **? –

+0

我想將文件另存爲數據庫 – user2847219

+0

您從logcat中刪除了該錯誤。所以我們不知道錯誤。 – greenapps

回答

1

我想說的問題是在這裏:

File internalDB = new File(str); 

0123的文件名正被設置爲你正在從緩衝區中讀取的內容,並且正在擲出:

android.system.ErrnoException: open failed: ENAMETOOLONG (File name too long) 
+0

我應該遵循哪種方式? – user2847219

+0

@ user2847219你的意思是'str'作爲文件名嗎?從你的代碼看起來,你可能打算使用'sd'作爲文件名。因此,文件internalDB =新文件(SD);而不是文件internalDB =新文件(str); – padonald