如何修复 android 中的 "download file store on google drive" 错误

How to fix "download file store on google drive" error in android

我制作了一个应用程序,用于将 pdf 文件从 link 直接下载到内部存储。当我尝试下载 google 驱动器 link 的直接 link 时,如果文件小于 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 文件,效果很好。但问题仅在于尝试从 google 驱动器下载时。请帮助我。

我能够从 google 驱动器下载大型 public 可共享文件。

使用 URL:

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

<FILE_ID> 替换为您的可共享文件 ID。

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

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

doInBackground函数里面的代码可以用,我根据自己的需要修改了它,改用了ProgressBar。我没有发布我的代码,因为它太长了。

希望能解决你的问题。