PDF Downlod and Ofline view
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
implementation 'com.airbnb.android:lottie:6.3.0'
implementation 'com.mindorks.android:prdownloader:0.6.0' } //-------
MainActivity -----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#BCCC25"
android:padding="10sp"
android:text="PDF One View"
android:textColor="#60BF46"
android:textSize="30dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="#395393"
android:padding="10sp"
android:text="PDF One View"
android:textColor="#33BAB6"
android:textSize="30dp"
/>
Expand
package com.shipon.pdflodanddownlod;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.downloader.Error;
import com.downloader.OnCancelListener;
import com.downloader.OnDownloadListener;
import com.downloader.OnPauseListener;
import com.downloader.OnProgressListener;
import com.downloader.OnStartOrResumeListener;
import com.downloader.PRDownloader;
import com.downloader.Progress;
public class MainActivity extends AppCompatActivity {
Button button1, button2;
int downloadId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PRDownloader.initialize(getApplicationContext());
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(v -> {
String pdfurl = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
File fil = new File(getCacheDir(), URLUtil.guessFileName(pdfurl, null, null));
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("isOnline", false);
intent.putExtra("pdfurl", pdfurl);
startActivity(intent);
}else {
showDialog(pdfurl);
}
button2.setOnClickListener(v -> {
String pdfurl = "https://www.clickdimensions.com/links/TestPDFfile.pdf";
File fil = new File(getCacheDir(), URLUtil.guessFileName(pdfurl, null, null));
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("isOnline", false);
intent.putExtra("pdfurl", pdfurl);
startActivity(intent);
}else {
showDialog(pdfurl);
}
}//============== onCreate end method =================
public void showDialog(String pdfUrl) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.choose_item, null);
Button btOnline = myView.findViewById(R.id.btOnline);
Button btDownlod = myView.findViewById(R.id.btDownlod);
Button btCancel = myView.findViewById(R.id.btCancel);
alert.setView(myView);
AlertDialog alertDialog = alert.create();
alertDialog.setCancelable(false);
btOnline.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("isOnline", true);
intent.putExtra("pdfurl", pdfUrl);
startActivity(intent);
btDownlod.setOnClickListener(v -> {
downlodPDF(pdfUrl);
alertDialog.dismiss();
});
btCancel.setOnClickListener(v -> {
alertDialog.show();
}// how Dialog end method --------
private void downlodPDF(String pdfurl) {
ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setIcon(R.drawable.down_24);
progressDialog.setMessage("Downloading PDF File, Please Wait a moment...");
progressDialog.setCancelable(false);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Downlod Start", Toast.LENGTH_SHORT).show();
PRDownloader.cancel(downloadId);
progressDialog.dismiss();
}
});
progressDialog.show();
downloadId = PRDownloader.download(pdfurl, String.valueOf(getCacheDir()), URLUtil.guessFileName(pdfurl, null, null))
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
}
})
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
int progressPercentage = (int) (progress.currentBytes*100/progress.totalBytes);
progressDialog.setMessage("Downlod : "+progressPercentage+" %");
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
Toast.makeText(getApplicationContext(), "Downlod Completed", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("isOnline", false);
intent.putExtra("pdfurl", pdfurl);
startActivity(intent);
progressDialog.dismiss();
}
@Override
public void onError(Error error) {
Toast.makeText(getApplicationContext(), "Downlod Failed", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}//============== public class ==========================
আমি এখানে দুইটি Activity niyaci
MainActivity finis
MainActivity2 start
Voting ended on Mar 5, 2024
MainActivity 2 xmal class start
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity2">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lotti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/pdf"
/>
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
MainActivity2.java class start
package com.shipon.pdflodanddownlod;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Toast;
import com.airbnb.lottie.LottieAnimationView;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity2 extends AppCompatActivity {
// public static String AssateName="";
LottieAnimationView lotti;
PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
pdfView = findViewById(R.id.pdfView);
lotti = findViewById(R.id.lotti);
lotti.setVisibility(View.VISIBLE);
boolean isOnline = getIntent().getBooleanExtra("isOnline", true);
String pdfurl = getIntent().getStringExtra("pdfurl");
if (isOnline == true) {
new RetrivePDFfromUrl().execute(pdfurl);
File fil = new File(getCacheDir(), URLUtil.guessFileName(pdfurl, null, null));
}//============= onCreate ned method ===================
private class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return new BufferedInputStream(httpURLConnection.getInputStream());
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
@Override
protected void onPostExecute(InputStream inputStream) {
super.onPostExecute(inputStream);
if (inputStream != null) {
lodPDFOnline(inputStream);
} else {
Toast.makeText(MainActivity2.this, "PDF lod failed", Toast.LENGTH_SHORT).show();
}
private void lodPDFOnline(InputStream inputStream) {
pdfView.fromStream(inputStream)
.enableSwipe(true)
.swipeHorizontal(true)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true)
.spacing(0)
.pageFitPolicy(FitPolicy.WIDTH)
.pageSnap(true) // snap pages to screen boundaries
.pageFling(true) // make a fling change only a single page like ViewPager
.onLoad(nbPages -> {
lotti.setVisibility(View.GONE);
pdfView.setVisibility(View.VISIBLE);
private void lodPDFOffline(File file) {
pdfView.fromFile(file)
.enableSwipe(true)
.swipeHorizontal(true)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true)
.spacing(0)
.pageFitPolicy(FitPolicy.WIDTH)
.pageSnap(true) // snap pages to screen boundaries
.pageFling(true) // make a fling change only a single page like ViewPager
.onLoad(nbPages -> {
lotti.setVisibility(View.GONE);
pdfView.setVisibility(View.VISIBLE);
}//= ==================== public calss end method =======================