PDF viewed can't open file - but only on tablet - android

I have an app that among other things fires up a pdf reader to allow the user to view a document selected by the app. It works on my phone but not Samsung tablet. The intent seems to work fine and the selection of reader apps comes up but when the reader is selected, a short time later the error message” Can’t open file “ is shown.
The same app can also fire up browser and text applications to show other files and this works fine on the tablet. So my file references are all OK. External storage for WRITE in the manifest is set OK.
When I select the document on the tablet (not thru my app) it opens OK. I have selected the pdf part of the main app and extracted it into a simple pdf reading only app and still the same problem.
It seems to be specific to the tablet – can anybody help me?
Intent i = new Intent(Intent.ACTION_VIEW);
File file1 = new File(Environment.getExternalStorageDirectory() + "/Documents/A.pdf");
Uri ur=Uri.fromFile(file1);
i.setType("application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i) ;

Instead of this code
Intent i = new Intent(Intent.ACTION_VIEW);
File file1 = new File(Environment.getExternalStorageDirectory() + "/Documents/A.pdf");
Uri ur=Uri.fromFile(file1);
i.setType("application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i) ;
Use this way
File pdfFile = new File(Environment.getExternalStorageDirectory(),"namePdfFile.pdf");//File path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
} else {
Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
}
To Read PDF in Browser Use below code :
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

Thanks to all for help. Actually I was just confused by the mime types. Read up on this and all is well. App working perfectly along the lines posted by Ironman

Related

How can I pass a PDF to Adobe Reader on Android by intent to edit ist directly and not a copy?

I use the following code to open a pdf form. If I use Acrobat Reader it's not possible to write back to the original file. It always makes a copy which I only could guess but newer know for shure in my app.
I know that it has to be possible to edit the original because if I open a pdf from any file manager (e.g. the one from Asus) Adobe Reader edits it directly.
Xodo PDF allows direct edit. It even supports the correct ACTION_EDIT intent but I'm afraid that our users insist to use Adobe...
How can I edit the original with Adobe?
final File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + File.separator + "x.pdf");
if(file.exists())
{
file.setWritable(true,false);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = FileProvider.getUriForFile(Objects.requireNonNull(getContext()),
BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setDataAndType(contentUri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent = Intent.createChooser(intent, "Open File");
startActivity(intent);
}
At the moment even Xodo doesn't work anymore but I was lucky to realized that OneDrive-PDF-Viewer does.

Android open PDF from URI starting content

I am trying to open up PDF using intent that takes the following:
Here is the code that I have:
Intent intent = new Intent(Intent.ActionVIEW)
intent.setDataAndType(Uri.parse("CONTENTURI + FILENAME","application/pdf"
try { startActivity(intent)} catch excpetion and so on.
It pops up with whatever applications I have installed, from Adobe Reader, Google PDF reader, POLARIS(as I am using Galaxy Tab 3 for testing), and none of those work. Each say unsupported document.
Does download and show the right solution in this case, or any ideas? Thanks in advance!
Following code triggers the VIEW action for pdf files (open the default PDF viewer, choose between applications capable to view PDFs or get an error if you don't have any installed).
File file = new File("your pdf path here");
if (file.exists()) {
Uri pdfPath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfPath, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
//if user doesn't have pdf reader instructing to download a pdf reader
}
}
NOTE: The PDF should not be saved local to application, or else the third party app won't have access. You should use media location.

Force browser chooser when opening a local html file in Android

Maybe this question or similar has been asked before, but I am really struggling for days now and couldn't find a precise answer and solution.. I have a local html file that I want to open with a browser chooser. So far I have this code:
File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "index/index.htm");
Uri webPageUri = Uri.fromFile(file);
browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(webPageUri, "text/html");
chooserIntent = Intent.createChooser(browserIntent, "Choose your app:");
if (browserIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooserIntent);
}
which mostly gives me the html viewer and file/text editor as options. Additionally to this options, on some devices one browser option appears (for example opera).
How to tell the chooser that my file is an html file, and force the browser chooser?
Please share...
Where's chooserIntent generated?
Replace
startActivity(whateverIntent)
with
startActivity(Intent.createChooser(browserIntent, "Open webpage using:"));

Android Open PDF Intent

I need to open a PDF. I know the procedure, this is my code:
string file_path = _path + url.Substring (5);
if (System.IO.File.Exists(file_path)) {
Android.Net.Uri pdfFile = Android.Net.Uri.FromFile (new Java.IO.File (file_path));
Intent pdfIntent = new Intent (Intent.ActionView);
pdfIntent.SetDataAndType (pdfFile, "application/pdf");
pdfIntent.SetFlags (ActivityFlags.NoHistory);
_parent.StartActivity (pdfIntent);
return true;
}
I'm using Xamarin, and the path exists because i check it as you can see.
The app opens Adobe reader, but when it starts an error message shows up saying (File not found). So, my file is in
/data/data/com.myapp/files/.hide/contents/file_test.pdf
Are there some permission to set? I really don't understand why it can't open my file!
The folder /data/data/com.myapp is the private folder of your application. So no other application can access to the content of this folder (in this case Adobe reader).
Instead try to put your pdf into a SDCard folder.

Opening PDF externally with Polaris office 5 returns "This document cannot be opened"

We've been trying to solve this issue for the past few hours, finally decided we should revert to StackOverflow.
Here goes -
We have an application that downloads a PDF file from a server to the app's cache directory and then opens it using the packet manager. Of course it goes through grantUriPermission() to give read (and write) permissions to all available packages.
Though it works great on most devices, today we encountered a device that has POLARIS Office 5 installed as the default PDF viewer.
Whenever we're opening the file, Polaris simply displays a message saying "This document cannot be opened".
I should say that when trying to open the file through Acrobat Reader, it works great.
Also, when we copied the file from the cache directory to an external directory (using Android's File manager) and then opened it in Polaris manually it worked great.
We would have given up on it, but since Polaris is the default viewer on many devices, we really would love solving that problem.
Here is the code -
public void onDownloadDone(String filepath) {
// Set a file object that represents the downloaded file
File file = new File(filepath);
// Set an intent for the external app
Intent intent = new Intent(Intent.ACTION_VIEW);
// Get mime type of the downloaded file
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(filepath);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
intent.setType(mimeType);
// Look for installed packges according to the file's mime type
PackageManager pm = context.getPackageManager();
Uri contentUri = FileProvider.getUriForFile(context, "myapp.fileprovider", file);
// Set the file uri in the intent
intent.setData(contentUri);
// Give permissions to the file to each external app that can open the file
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo externalApp: activities){
String packageName = externalApp.activityInfo.packageName;
context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
// Start the activities (or launch the menu) if any activity exists
if (activities.size() > 0){
context.startActivity(intent);
} else {
System.out.println("Warning!!! No app for file " + filepath);
}
}
Thank a lot!
I had exactly the same problem. The PDF files would open with ezPDF and Adobe but not with Polaris Viewer.
You have to set the data and type with:
intent.setDataAndType(uri, "application/pdf");
instead of using:
intent.setType("application/pdf");
intent.setData(uri);
For me the following is working fine with Polaris now:
Uri uri = FileProvider.getUriForFile(MyApplication.getContext(), MyApplication.getContext().getPackageName() + ".myfileprovider", destFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

Categories

Resources