Android Drive api下载文件 [英] Android Drive api Download file

查看:126
本文介绍了Android Drive api下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功使用此项目在Google云端硬盘中创建或修改文件。现在我需要将文件下载到设备的外部存储器。我可以读取文件的内容,并且可以保存它。但是,当我尝试在桌面上打开它时,文件已损坏。

I'm successfully using this project to create or modify a file in Google Drive. Now I need to download the file to the external memory of device. I can read the contents of the file, and I can save it. But when I try to open it in the desktop, the file is corrupted.

    @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;
    }


推荐答案

使用此代码从Google Drive

use this code to download from google drive

您只需传递您想要保存在SD卡上的driveId和文件名称

you just need to pass driveId and file name that you want to save on sd card

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

这篇关于Android Drive api下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆