I have a pdf file stored on my sd card named as "test.pdf". I have opened this file in my application using following code:
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ "test.pdf");
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
Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
}
}
});
PDF is displayed inside my application. Now i want to print this PDF file using my shared printer. How can I do that?
Related
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'm trying to display a local pdf file on my android app but it says that the pdf file doesn't exists ,the pdf file it's in the project's assets folder
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programa);
verPdf = (Button)findViewById(R.id.button1);
verPdf.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Uri file= Uri.parse("file:///assets/miarchivo.pdf");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));
try{
Intent i;
i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(file,mimeType);
startActivity(i);
}catch (ActivityNotFoundException e) {
Toast.makeText(v.getContext(),
"No Application Available to fiew this file type",
Toast.LENGTH_SHORT).show();
}
}
});
}
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.
Here is my button click code:
private View.OnClickListener onButton1=new View.OnClickListener() {
public void onClick(View v){
copyAssets();
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/sdcard/" +"save.pdf");
if(file.exists())
{
// do action here
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "File does not exist", Toast.LENGTH_LONG).show();
}
}
};
copyAssets() is a function by which I copy the file from my assets folder to my SD-Card and it is working as I checked on my device, but I am unable to open it when I click the button. My file is named as save.pdf on the SD-Card
you can read pdf file from SDCard by giving the path as follows:
File pdfFile = new File(path);
if(pdfFile.exists())
{
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(uractivity.this, "File does not exist", Toast.LENGTH_LONG).show();
}
}
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.