I want to load a pdf file from external storage (Download/Pdfs/myfile.pdf) using AndroidPdfViewer but it shows blank screen without any error. I tried lots of ways but it's not working.
public class PdfViewActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromFile(path).load();
I have a pdf file in my "Download/Pdfs/myfile.pdf" and i used the above code to load the file but it's not working. I have given storage permission manually from settings. Can anyone please correct me where i am making a mistake.
I have tested your code and it works just fine on Android 10 device. Your are missing something from the below:
1.In Android Manifest File add the READ_EXTERNAL_STORAGE permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and inside application tag add requestLegacyExternalStorage to true to be able to have access on External Storage on Android 10 device and above.
<application
android:requestLegacyExternalStorage="true"
2.Verify that the pdf exists on the device under "/Download/Pdfs/myfile.pdf" path.
3.Change your activity using the below code by requesting External Storage permission at runtime first in onCreate method:
public class PdfViewActivity2 extends AppCompatActivity {
private static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//check if Read External Storage permission was granded
boolean granded = checkPermissionForReadExtertalStorage();
if(!granded){
requestPermissionForReadExtertalStorage();
}
else {
readPdf();
}
}
public boolean checkPermissionForReadExtertalStorage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int result = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
return false;
}
public void requestPermissionForReadExtertalStorage() {
try {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_STORAGE_PERMISSION_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case READ_STORAGE_PERMISSION_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted. Read Pdf from External Storage
readPdf();
} else {
// permission denied. Disable the functionality that depends on this permission.
}
}
}
}
private void readPdf(){
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromFile(path).load();
}
}