I try to open pdf, but when I press the button, nothing happens.
Where is my mistake?
OnClickListener oclBt2 = new OnClickListener(){
public void onClick(View v) {
File file = new File("http://testserv1.p.ht/1/ksu016.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
}
};
I corrected my code, but it doesn't work again :( when I press the button, appears the window(Sorry, but my reputation doesn't allow to post images)
OnClickListener oclBt2 = new OnClickListener(){
public void onClick(View v) {
Uri path = Uri.parse("http://testserv1.p.ht/1/ksu016.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=http://hostforandroid.elitno.net/pdf-test.pdf" );
setContentView(mWebView);
}
}
};
First, File is for local files, not http URLs. Use Uri.parse("http://testserv1.p.ht/1/ksu016.pdf"); to get a Uri pointing to an http URL.
Second, there may be no PDF viewers that are set up to directly download from an HTTP URL. For greater compatibility, you can arrange to download the PDF first (using DownloadManager or your own HTTP client code), then view the local PDF file.
Related
I have an app where on button click, access should be provided to browse and open the PDF files stored in the internal memory of the device.I tried various codes but i get an toast saying "Cannot display PDF". I don't get any errors in logs. I use android version 7.0 device. Attached my code below: please help:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I have included this in Oncreate:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
This is how called the method:
linear_pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pdf.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
openPdf(getContext(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
communication.dismiss();
}
});
Try this one...
public void openPdf(Context context, String path){
File file = new File(path);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
PackageManager pm = context.getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("application/pdf");
Intent openInChooser = Intent.createChooser(intent, "Choose");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
if (resInfo.size() > 0) {
try {
context.startActivity(openInChooser);
} catch (Throwable throwable) {
Toast.makeText(context, "PDF apps are not installed", Toast.LENGTH_SHORT).show();
// PDF apps are not installed
}
} else {
Toast.makeText(context, "PDF apps are not installed", Toast.LENGTH_SHORT).show();
}
}
}
Call this method:
openPdf(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
For opening one specific file:
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/your_directory/" + "your_file_name" + ".pdf");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(getContext(), "No required app", Toast.LENGTH_SHORT).show();
}
Check directory, this one works.
For opening pdf file from local storage add prefix in url.
private void openPdf(File file) {
try {
String url = file.getAbsolutePath();
String u = "file:///" + url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(u));
startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
}
In my app I created a hidden text file using below code:
logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt");
if(!logfile.exists()){
try {
logfile.createNewFile();
//Toast.makeText(SimpleIME.this,"File created...",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(SimpleIME.this,"IOException : "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
This works fine. It creates a hidden file. And then again I want to open that text file when i press a button called viewlog.
Code for viewlog goes like this.
viewlog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt");
Uri uri = Uri.parse("file://" + logfile.getAbsolutePath());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
});
So when I run this app and when I click this viewlog button it force closes the app.
So how to fix this problem?
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent
logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt");
Uri uri = Uri.parse("file://" + logfile.getAbsolutePath());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/plain");
startActivity(intent);
I want to open a pdf file via an Android app. So far I have written the following code:
public class PdfopenActivity extends Activity {
String selectedFilePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Sample.pdf is my file name it is in /Root/Download/Sample.pdf
// path of the file
File file = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sample.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
}
The pdf is 175kb and I can open the file directly on my Galaxy Tab2 tablet, but when I run my my program to open it I get the error:
An Error Occurred while opening the document.
Can anyone tell me where I am going wrong?
.
Try this out:
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/sample.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
}
}
}
});
Hope this will help you.
I want to open a pdf from a website onto an app i'm making for the kindle and android.
I think my code is right but it can't find my file and I wasn't sure if anyone knew if I should be formatting the file different since it's technically a website....
Any input would be greatly appreciated. Here's what I have so far:
Button OpenPDF = (Button) findViewById(R.id.button1);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("http://thisisthewebsitewithpdf.pdf");
if(pdfFile.exists()) //EXCEPTION HERE. pdfFile doesn't exist!
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(PDFActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
you can download the pdf to your sdcard and then open it using this activitty:
public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
How to Read PDF in Android stored in SDCard ??
Here's some code showing how to open a pdf file to read:
private void openBook() {
File file = new File(mRealPath);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getString(R.string.application_type));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(FirstTab.this,
getString(R.string.no_application_found),
Toast.LENGTH_SHORT).show();
}
}