Open A Pdf File Programmatically
Solution 1:
I got solution of above problem, so try once;
Steps:-
create assets folder in src under your app name.
In this assets folder keep your pdf files e.g. schedule1.pdf.
now come your activity i.e MainActivity.java
setListener on any UI component what you want i.e (
Button
,ImageView
,ImageButton
);In this listener call one user defined method i.e.
openPDFFiles()
the openPDFFiles() method have below code:
privatevoidopenPDFFiles() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), “schedule1.pdf”);//here schedule1.pdf is the pdf file name which is keep in assets folder.try {
in = assetManager.open(“schedule1.pdf”);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e(“tag”, e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(“file://” + getFilesDir() + “/schedule1.pdf”), “application/pdf”);
startActivity(intent);
}
privatevoidcopyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = newbyte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
Solution 2:
Try LuxuryMode's method: https://stackoverflow.com/a/8221594/1500067
I think your just missing the adobe package intent.setPackage("com.adobe.reader");
Solution 3:
for android 9 and 10
you should use this code
1-create one class
publicclassGenericFileProviderextendsFileProvider {
}
2-in res
directory create one directory to name of xml
and create one file to name of provider_paths.xml
and add this code
<pathsxmlns:android="http://schemas.android.com/apk/res/android"><external-pathname="external_files"path="."/></paths>
3-in manifests
in application
add this code
<providerandroid:name=".Model.Utilitys.GenericFileProvider"android:authorities="${applicationId}.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider>
4-you should get Permission Manifest.permission.READ_EXTERNAL_STORAGE
5-and add this codes
String fileName="yourfile.pdf";
Filefile=newFile(Environment.getExternalStorageDirectory()+"/Android/data/ir.tdaapp.paymanyar/files/File",fileName);
Stringextension= MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
StringmimeType= MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_NEW_TASK);
Uriuri= FileProvider.getUriForFile(getContext(), getActivity().getApplicationContext().getPackageName() + ".provider", file);
intent.setDataAndType(uri, mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "choseFile"));
6-I suggest you add this code to the application
in manifests
android:requestLegacyExternalStorage="true"
Solution 4:
I have nearly identical code that works fine, though I'm not opening a file from SD card in my app.
ActivitymActivity=/* your activity */...;
StringmFileName=/* path of my PDF file */...;
Uriuri= Uri.fromFile(mActivity.getFileStreamPath(mFileName));
try
{
IntentintentUrl=newIntent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(uri, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
so your approach is right on. Make sure you can open the file first ... i.e. use mActivity.openFileInput() to ensure you have a readable PDF.
Solution 5:
You can achieve this using 3rd party library integration. Working libraries are listed below, with SDK
https://github.com/JoanZapata/android-pdfviewhttps://github.com/jblough/Android-Pdf-Viewer-Library
with NDK
https://code.google.com/p/mupdf/downloads/detail?name=mupdf-1.2-source.zip&can=2&q=
guide to use @
http://dixitpatel.com/integrating-pdf-in-android-application/
Post a Comment for "Open A Pdf File Programmatically"