I want to be able to open a pdf file from my application without any other application like pdf viewer. I don't want to make the user install an other appliation to open pdf files.
Is there any open source library that I can use in my project?
You can open a PDF in a webview using google docs. The URL format is
https://docs.google.com/gview?embedded=true&url=http://link.to.your/pdf_file.pdf
You can use, for instance, MuPdf Reader. I described how to build it here.
You can open a PDF in a webview using google docs. The URL format is
https://docs.google.com/gview?embedded=true&url=http://link.to.your/yourfile.pdf
or you can open pdf using pdf viewer Follow this code
FileFinalpath = SdCardpath + "/" + Filepath + Filename;
File file = new File(FileFinalpath);
if (file.exists()) {
Uri filepath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(filepath, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (Exception e) {
alert.showAlertDialog(PDF_Activity.this, "File Not Started...","File Not Started From SdCard ", false);
Log.e("error", "" + e);
}
} else {
alert.showAlertDialog(PDF_Activity.this, "File Not Found...","File Not Found From SdCard ", false);
}
Related
I have a word document(.doc or .docx) in the server. I need to open in my android application by clicking one button .doc file have to open. How to do this? anyone, please help me.
I tried this code :
File file = new File(Environment.getExternalStorageDirectory(),
"Document.docx");
try {
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
// replace "application/msword" with
// "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
// for docx files
// objIntent.setDataAndType(path,"application/msword");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(PMComplitionProjectActivity.this, "File NotFound",
Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(PMComplitionProjectActivity.this,
"No Viewer Application Found", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
}
I have this link that shows how to open any file in Android : http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html
Also, there is a similar case, which is done by PDF file : Android : how to open pdf file from server in android
I hope these links will work for you.
Good Luck,
G.
hello i want to show pdf file in webview or in external app.
1) i use this url
https://docs.google.com/gview?embedded=true&url= +pdfurl , but google says "Whoops There was a problen while previewing document"
2) if i use intent to open the url in external app then they didnot load the file in external app.
3) if i use the intent to open the url in browser then its start downloading the pdf file.
Intent ii=new Intent(Intent.ACTION_VIEW);
ii.setDataAndType(Uri.parse(pdf),"text/html");
try {
startActivity(ii);
}catch (ActivityNotFoundException e)
{
Toast.makeText(QuranPara.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
}
here is the complete url https://docs.google.com/gview?embedded=true&url=www.elibrary.alnoorpk.com/PDF%20Books/Lafzi_aur_bamahawrah_terjma_2015/Para%201%20(Lafzi%20aur%20Bamuhawra%20Tarjma).pdf
check the image
To show it in WebView try something like this
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);
To show it in browser try something like this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
If your files are in assets first copy them to the external storage. Hope that you have handled permission flow.
Try the following code for offline
File pdfFile = new File(Environment.getExternalStorageDirectory(),"pdfName.pdf");//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();
}
Try the following for the webview option if you choose that
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "online pdf link";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
But Remember if you use google docs for viewing the pdf or any other doc, google docs may throw you the error stating
You've reached the bandwidth limit for viewing or downloading files that aren't in Google Docs format..... So doesn't seem reliable
So, be cautious with google doc viewing
I have a string (called comments) that contains some text that I want to display using an external app. I initially create the file like so:
String end = "rtf";
FileOutputStream outputStream;
try {
outputStream = openFileOutput("document." + end, Context.MODE_PRIVATE);
outputStream.write(comments.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
However I am unable to open the file with an external application when I try the following:
String type = "text/rtf";
Intent intent = new Intent (Intent.ACTION_VIEW);
File file = new File(getFilesDir() + "/document." + end);
Uri fileUri = Uri.fromFile(file);
intent.setDataAndType(fileUri,type);
startActivityForResult(intent, Intent.FLAG_GRANT_READ_URI_PERMISSION);
The message that I receive when I open try to the document with the external app is:
"open failed: EACCESS (Permission denied)."
Please advise. Thanks.
However I am unable to open the file with an external application when I try the following:
Correct. Intent.FLAG_GRANT_READ_URI_PERMISSION is for use with a ContentProvider, not for bare file:// Uri values, such as you are using. Use FileProvider to add such a ContentProvider to your app. See also the "Sharing Files" training module and this sample app.
Bear in mind that there's a good chance that your next problem will be an ActivityNotFoundException, as relatively few Android devices will have an app that will support the text/rtf MIME type.
Few devices in android unable to read .crt (Certificate File) whereas few can. How to deal with this device related issue ? Is there any other Intent to open file other than ACTION_VIEW which can be an alternative to open file.
File vpnCerti = new File("/sdcard/VPNCertificate/Install.crt");
Uri path = Uri.fromFile(vpnCerti); //Mime Type Info : http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm
MimeTypeMap type_map = MimeTypeMap.getSingleton();
//Get the extension from the path
String extension = MimeTypeMap.getFileExtensionFromUrl(path.toString());
extension = extension.toLowerCase();
if (extension.contains(".")) {
extension = extension.substring(extension.lastIndexOf("."));
}
String mime_type = type_map.getMimeTypeFromExtension(extension);
Log.d("DownloadManager", "MIME Type : " + mime_type);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vpnCerti), mime_type);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
Log.d("DownloadManager", "Trying to open file");
startActivity(i);
Log.d("DownloadManager", "Opened file");
}
catch(ActivityNotFoundException e)
{
Log.d("DownloadManager", "Couldn't open file");
Toast.makeText(SettingsActivity.this, "Couldn't find specific activity to open it", Toast.LENGTH_LONG).show();
}
I am not sure about the exact problem you are encountering right now (without the stack trace it is very hard to tell), but it may be because some of your devices don't have a SD card installed. In fact, I have been researching for the best solution to load a certificate, and if you only have a few certificates to manage, putting them in /res/raw/ folder might be a better idea than storing them in sd card. Here is how to load one from /res/raw/:
InputStream is = context.getResources().openRawResource(R.raw.cert_id);
hi i am new to android development. i manually kept a pdf file in emulator sdcard through ddms, and i also installed "adobe reader" in emulator when i tried to read the pdf file in emulator
with the following code
File file =
new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/iTunes Connect.pdf");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path,"application/pdf");
try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(xv.this,
getString(R.string.app_name),
Toast.LENGTH_SHORT).show();
}
i am getting the file path is not valid error.
can any one help me in this.
use %20(for white space) in between iTunes and 20Connect.pdf.
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/iTunes%20Connect.pdf");
I think this will solve your problem
try out this path
File file=new File("/sdcard/iTunes Connect.pdf");
I think this will work for u
I think you should not use hard coded file paths.
The framework will give you the base path of the area you want to save files to.
For SD card you should, use Environment.getExternalStorageDirectory()
local files you should, use Context.getFilesDir() (or Context.openFileOutput(String name, int mode), etc)
local cache you should, use Context.getCacheDir()
For emulator you can try File file=new File("mnt/sdcard/iTunes Connect.pdf");
I think that your emulator have not adob reader install please check first it is install or not
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
} `enter code here`