2016-05-24 17 views
4

Ich verwende erfolgreich this project, um eine Datei in Google Drive zu erstellen oder zu ändern. Jetzt muss ich die Datei auf den externen Speicher des Geräts herunterladen. Ich kann den Inhalt der Datei lesen, und ich kann es speichern. Aber wenn ich versuche, es auf dem Desktop zu öffnen, ist die Datei beschädigt.Android Drive api Datei herunterladen

@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 = buf == null ? "" : new String(buf); 
        File fl = UT.str2File(str, "myfile.db"); 


         } 
    ---------------------------------------------- 
    static File str2File(String str, String name) { 
    if (str == null) return null; 
    byte[] buf = str.getBytes(); 

    File fl = new File(Environment.getExternalStorageDirectory(), name); 

    if (fl == null) return null; 
    BufferedOutputStream bs = null; 
    try { 
     bs = new BufferedOutputStream(new FileOutputStream(fl)); 
     bs.write(buf); 
    } catch (Exception e) { le(e); } 
    finally { 
     if (bs != null) try { 
     bs.close(); 
     } catch (Exception e) { le(e); } 
    } 
    return fl; 
    } 

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

Sie auf sD-Karte speichern möchten einen ByteArrayOutputStream statt Fileoutputstream erstellen haben. –

Antwort

3

Gebrauch Gesicht soll zum Download Fahrt von google

Sie brauchen nur DriveID und Dateinamen zu übergeben, die Sie

private void DownloadFile(final DriveId driveId, final File filename) { 
     AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       if (!filename.exists()) { 
        try { 
         filename.createNewFile(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      @Override 
      protected void onPostExecute(Boolean result) { 
       super.onPostExecute(result); 

      } 

      @Override 
      protected Boolean doInBackground(Void... params) { 
       DriveFile file = Drive.DriveApi.getFile(
         GapiClient, driveId); 
       file.getMetadata(GapiClient) 
         .setResultCallback(metadataRetrievedCallback); 
       DriveApi.DriveContentsResult driveContentsResult = file.open(
         GapiClient, 
         DriveFile.MODE_READ_ONLY, null).await(); 
       DriveContents driveContents = driveContentsResult 
         .getDriveContents(); 
       InputStream inputstream = driveContents.getInputStream(); 

       try { 
        FileOutputStream fileOutput = new FileOutputStream(filename); 

        byte[] buffer = new byte[1024]; 
        int bufferLength = 0; 
        while ((bufferLength = inputstream.read(buffer)) > 0) { 
         fileOutput.write(buffer, 0, bufferLength); 
        } 
        fileOutput.close(); 
        inputstream.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       return true; 
      } 

     }; 
     task.execute(); 
    } 

    private ResultCallback<MetadataResult> metadataRetrievedCallback = new ResultCallback<MetadataResult>() { 
     @Override 
     public void onResult(MetadataResult result) { 
      if (!result.getStatus().isSuccess()) { 
       return; 
      } 
      metadata = result.getMetadata(); 
     } 
    }; 
+0

Wie erhält man Dateinamen basierend auf driveId? irgendeine Idee ? –

+1

Ich denke, Sie erhalten Datei von dieser Zeile >> DriveFile-Datei = Drive.DriveAPi.getFile (GapiClient, driveId); Bitte explore Datei und ich hoffe, Sie erhalten den Dateinamen. –

0

ist dies für mich gearbeitet:

@Override 
    protected String doInBackground(String... params) { 
     try { 
      //connecting to url 
      URL u = new URL(fileURL); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      if (!rootDir.exists()) { 
       rootDir.mkdirs(); 
      } 

      //lenghtOfFile is used for calculating download progress 
      int lenghtOfFile = c.getContentLength(); 

      //this is where the file will be seen after the download 
      FileOutputStream f = new FileOutputStream(new File(rootDir, fileName)); 
      //file input is from the url 
      InputStream in = c.getInputStream(); 


      //here’s the download code 
      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      long total = 0; 

      while ((len1 = in.read(buffer)) > 0) { 
       total += len1; //total = total + len1 
       f.write(buffer, 0, len1); 
      } 
      f.close(); 

     } catch (Exception e) { 
      Log.d(LOG_TAG, e.getMessage()); 

     } 

     return null; 
    } 
0

Sicherungsdatei mit der Erweiterung. gleiche passierte mir

zB wenn Ihr Dateiname ist xyz und Sie es speichern, ohne Verlängerung manchmal Dinge schief gehen, wenn Sie Datei mit der Erweiterung speichern sagen xyz.pdf Sie dieses Problem nicht diesen Code

Verwandte Themen