Created
December 5, 2011 09:26
-
-
Save jimmy-collazos/1432988 to your computer and use it in GitHub Desktop.
Snippet/Android: Open PDF file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//see http://stackoverflow.com/questions/2883355/how-to-render-pdf-in-android/ | |
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(); | |
} | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment