I've made an app for downloading a pdf file from direct link to internal storage. When I try to download a direct link of google drive link it works fine, if the file is less than 3MB. But if the file is more than 3MB, it is not downloaded. Here is my code below:
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();
}
}
}
What is the error in this code? How can I solve this?
If I try to download a pdf file from another site, it works well. But the problem is only, when trying to download from google drive. please help me.