Android open pdf file
The problem is that there is no app installed to handle opening the PDF. You should use the Intent Chooser, like so:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
Opening PDF file using Android intent
Please set intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
It will grant access of your file to other providers
Android open pdf file via Intent
I think you forgot to specify the file. See your code, you only pointed it to the folder, but no the file itself. I think that is why it tells you that it is in wrong format, because it does not end with .pdf
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "SOMEFOLDER" + File.separator + "pdffile.pdf");
EDIT: Modify methods according to your comment
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
String fileName = FilesInFolder.get(position);
open_File(fileName);
}
});
public void open_File(String filename){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SOMEFOLDER", filename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent1 = Intent.createChooser(intent, "Open With");
try {
startActivity(intent1);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
Intent to open PDF file in Android N
Add FLAG_GRANT_READ_URI_PERMISSION
on the Intent
in your FileProvider
case. Otherwise, the other app has no access to the content. See the documentation.
Limitations on opening pdf file in Android
I found a workaround to view my PDF on my Android application (but does not allow me to download it after show on the application). If I open my PDF using Google Docs I can open it with my Intent.
This is what I had to add before my url:
https://docs.google.com/gview?embedded=true&url=
so the final url will be like:
https://docs.google.com/gview?embedded=true&url=https://myUrl.pdf
Here is the whole code I am using right now:
String url= "https://docs.google.com/gview?embedded=true&url=https://url.pdf";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
But it is still not enough, because I would like to open it without need of using a third party application. Also, opening PDF with Google Docs is slow and I have to wait too much until the PDF is finally opened.
If anyone knows how to open it with native Android please let me know.
What happens if I do not open it with Google Docs?
With the same code, but just using my url, instead the added Google Docs url, Android let me choose between two applications: Adobe Acrobat Reader and Google Chrome.
If I open it with Google Chrome it download the PDF but it is not opened automatically.
If I open it with Adobe Acrobat Reader, it gives to me the following error:
The PDF cannot be shown (it cannot be opened)
Show PDF file in App
Android provides PDF API now with which it is easy to present pdf content inside application.
you can find details here
Below is the sample snippet to render from a pdf file in assets folder.
private void openRenderer(Context context) throws IOException {
// In this sample, we read a PDF from the assets directory.
File file = new File(context.getCacheDir(), FILENAME);
if (!file.exists()) {
// Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
// the cache directory.
InputStream asset = context.getAssets().open(FILENAME);
FileOutputStream output = new FileOutputStream(file);
final byte[] buffer = new byte[1024];
int size;
while ((size = asset.read(buffer)) != -1) {
output.write(buffer, 0, size);
}
asset.close();
output.close();
}
mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
// This is the PdfRenderer we use to render the PDF.
if (mFileDescriptor != null) {
mPdfRenderer = new PdfRenderer(mFileDescriptor);
}
}
update: This snippet is from google developers provided samples.
Open online pdf file through android intent?
You can view or download the pdf by either of the two ways i.e by opening it in device in-built browser or in the webview by embedding it in your app.
To open the pdf in browser,
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
Instead to open in webview,
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
Related Topics
How to Keep the Spaces at the End And/Or at the Beginning of a String
How to Add a Fast-Scroller to the Recyclerview
Android Recyclerview:Notifydatasetchanged() Illegalstateexception
How to Display Image in Android's Textview
How to Set Text in an Edittext
How to Use the Same Keystore File to Sign Two Different Applications
How to Make Android Apps Which Support Both 32-Bit and 64-Bit Architecture
Android - How to Achieve Setonclicklistener in Kotlin
Differencebetween Background, Backgroundtint, Backgroundtintmode Attributes in Android Layout Xml
Show and Hide a View With a Slide Up/Down Animation
How to Get Color-Int from Color Resource
Adb Install Fails with Install_Failed_Test_Only
How to Filter Android Logcat by Application
How to Display Progress While Loading a Url to Webview in Android
Textview with Different Textsize
Why Mediaplayer Throws Not Present Error When Creating Instance of It