【问题标题】:How to fix "download file store on google drive" error in android如何修复Android中的“在谷歌驱动器上下载文件存储”错误
【发布时间】:2019-06-07 15:55:27
【问题描述】:

我制作了一个应用程序,用于从直接链接下载 pdf 文件到内部存储。当我尝试下载 google drive 链接的直接链接时,如果文件小于 3MB,它可以正常工作。但如果文件超过 3MB,则不会下载。下面是我的代码:

public class MainActivity extends AppCompatActivity {
private final  String Pdf_LINK = 
("https://drive.google.com/uc?export=download&id=13mE9gCyTGmLrFOZqu6Lz-yz0mcfjGoJc");
private final String My_PDF ="my100.pdf";
private AppCompatSeekBar seekBar;
private PDFView pdfView;
private TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pdfView = findViewById(R.id.pdfView);
    txtView = findViewById(R.id.txtView);
    initSeekar();
    downloadpdf(My_PDF);
}
private void initSeekar(){
seekBar = findViewById(R.id.seeBar);
seekBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
seekBar.getThumb().setColorFilter(Color.RED,PorterDuff.Mode.SRC_IN);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int val = (progress * (seekBar.getWidth() - 3 * seekBar.getThumbOffset())) / seekBar.getMax();
        txtView.setText("" + progress);
        txtView.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});
}

private void downloadpdf(final String fileName) {
    new AsyncTask<Void, Integer, Boolean>() {
        @Override
        protected Boolean doInBackground(Void... params) {return downloadpdf();}

        @Nullable
        private Boolean downloadpdf() {
            try {
                File file = getFileStreamPath(fileName);
                if (file.exists())
                    return true;
                try {
                    FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
                    URL u = new URL(Pdf_LINK);
                    URLConnection conn = u.openConnection();
                    int contentLength = conn.getContentLength();
                    InputStream input = new BufferedInputStream(u.openStream());
                    byte data[] = new byte[contentLength];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {

                        total += count;
                        publishProgress((int) ((total * 100) / contentLength));
                        fileOutputStream.write(data, 0, count);
                    }
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    input.close();
                    return true;
                } catch (final Exception e) {
                    e.printStackTrace();

                    return false;
                    }
                } catch (Exception e) {
                e.printStackTrace();
                    }
                     return false;
                }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            seekBar.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (aBoolean) {
                openPdf(fileName);
            } else {
                Toast.makeText(MainActivity.this, "Unable to download this file", Toast.LENGTH_SHORT).show();

                }
            }
        }.execute();
    }


    private void openPdf(String fileName) {
    try {
        File file = getFileStreamPath(fileName);
        Log.e("file", "file: " + file.getAbsolutePath());
        seekBar.setVisibility(View.GONE);
        pdfView.setVisibility(View.VISIBLE);
        pdfView.fromFile(file)
                  .enableSwipe(true)
                  .swipeHorizontal(false)
                  .load();
    } catch (Exception e) {
          e.printStackTrace();
          }
    }
}

这段代码有什么错误?我该如何解决这个问题?
如果我尝试从其他站点下载 pdf 文件,它会运行良好。但问题仅在于尝试从谷歌驱动器下载时。请帮帮我。

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题。
  • 还没有,但我正在尝试。
  • 尝试使用?而不是 &在您的网址中。

标签: android download android-download-manager


【解决方案1】:

我能够从谷歌驱动器下载大型公共共享文件。

使用网址:

https://drive.google.com/uc?id=<FILE_ID>&export=download 

用您的可共享文件 ID 替换 &lt;FILE_ID&gt;

我使用了'private class DownloadTask'中的代码 在这个解决方案中:

Download a file with Android, and showing the progress in a ProgressDialog

doInBackground 函数内的代码有效,我根据自己的需要对其进行了修改,改用 ProgressBar。我没有发布我的代码,因为它太长了。

希望你能解决你的问题。

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多