Sei sulla pagina 1di 3

package com.example.mahmod.

pdfreader;

import java.io.File;
import android.app.Activity;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

import com.example.mahmod.pdfreader.R;
public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.bPressMe);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

File pdfFile = new File(Environment


.getExternalStorageDirectory(), "VBCodes.pdf");

try {

if (pdfFile.exists()) {

Uri path = Uri.fromFile(pdfFile);

Intent objIntent = new Intent(Intent.ACTION_VIEW);

objIntent.setDataAndType(path, "VBCodes.pdf");

objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(objIntent);

} else {
Toast.makeText(MainActivity.this, "File NotFound",

Toast.LENGTH_SHORT).show();

} catch (ActivityNotFoundException e) {

Toast.makeText(MainActivity.this,

"No Viewer Application Found",


Toast.LENGTH_SHORT)

.show();

} catch (Exception e) {

e.printStackTrace();

});

XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#FFFFFF">

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"

android:layout_marginTop="124dp"

android:text="Welcome to AndroidBite !!!"

android:textColor="@android:color/black"

android:textSize="20sp" />

<Button

android:id="@+id/bPressMe"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:text="Press Me !!!" />

</RelativeLayout>

Potrebbero piacerti anche