// this is code works in Real device only
{
@Override
public void onCreate(Bundle savedInstanceState)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
File file = new File("/sdcard/example1.pdf"); // file path
if (file.exists()) // if file exists
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
File file = new File("/sdcard/example1.pdf"); // file path
if (file.exists()) // 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) // if i get exception
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) // if i get exception
{
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
Log.w(getClass().getName(), e.toString());
}
} else {
Toast.makeText(OpenPdf.this, "File Doesn't Exists ",
Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
Log.w(getClass().getName(), e.toString());
}
} else {
Toast.makeText(OpenPdf.this, "File Doesn't Exists ",
Toast.LENGTH_SHORT).show();
}
}
// Note : this code won't works in Emulator
0 komentar:
Post a Comment