0

I have Drive Id of selected file and I am able to get Url of that file using

MetadataResult mdRslt;
        DriveFile file;
    file = Drive.DriveApi.getFile(mGoogleApiClient,driveId);
                mdRslt = file.getMetadata(mGoogleApiClient).await();
                if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
                    link = mdRslt.getMetadata().getWebContentLink();
                    if(link==null){
                        link = mdRslt.getMetadata().getAlternateLink();
                        Log.e("LINK","FILE URL After Null: "+ link);
                    }
                    Log.e("LINK","FILE URL : "+ link);
                }

How to download file from url and save in to SD card? Please help me regarding this. Thanks.

Harry
  • 369
  • 2
  • 17

2 Answers2

2

UPDATE:
Actually, since you writing it to a file, you don't need the 'is2Bytes()'. Just dump the input stream (cont.getInputStream()) directly to a file.

ORIGINAL ANSWER:
Since you are referring to the GDAA, this method (taken from here) may just work for you:

  GoogleApiClient mGAC;

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

  byte[] is2Bytes(InputStream is) {
    byte[] buf = null;
    BufferedInputStream bufIS = null;
    if (is != null) try {
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
      bufIS = new BufferedInputStream(is);
      buf = new byte[4096];
      int cnt;
      while ((cnt = bufIS.read(buf)) >= 0) {
        byteBuffer.write(buf, 0, cnt);
      }
      buf = byteBuffer.size() > 0 ? byteBuffer.toByteArray() : null;
    } catch (Exception ignore) {}
    finally {
      try {
        if (bufIS != null) bufIS.close();
      } catch (Exception ignore) {}
    }
    return buf;
  }

It is a simplified version of 'await' flavor that has be run off-UI-thread. Also, dumping input stream into a buffer is optional, I don't know what your app's needs are.

Good Luck.

seanpj
  • 6,735
  • 2
  • 33
  • 54
  • Thanks for your reply. I tried this. Some selected file input stream is returning null. I notice that it is happening in the case in which metadata.getFileExtension() is returning null. and if it is returning correct extension like png,docx then I am able to get its Input stream and download that file. Please suggest me on this. Thanks. – Harry Aug 03 '15 at 12:15
  • The 'selected file' notion is too vague to figure out what your problem is. Consider another SO question with code sample. Your code above starts with DriveId, which does not indicate how you got it. Remember though, the file extension does not mean anything, look at file's / folder's MIMETYPE. – seanpj Aug 03 '15 at 13:04
0

Use downloadUrl or one of the exportLinks. See https://developers.google.com/drive/v2/reference/files for a description of these properties.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • 1
    I gave him a GDAA version as to limit the confusion. Mixing the two can be dangerous to his app's health. – seanpj Aug 01 '15 at 12:00