Breaking News
Loading...

android: open a pdf from my app using the built in pdf viewer

12:15 PM
// this is code works in Real device only

public class OpenPdf extends Activity implements OnClickListener
{
@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
{
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();
}

}

// Note : this code won't works in Emulator

0 komentar:

Post a Comment

 
Toggle Footer