tags:

views:

37

answers:

0

Hello, I am wondering if my codes here can view a specific pdf file using existing PDF viewer in Android Phone (HTC Desire).. If i would like to open pdf files from local folder.. What should i do?

public class ghcm_Submenu1 extends Activity {

private ListView lv1;
private String lv_arr[]= {"item1", "item2"};

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.submenu);

        lv1=(ListView)findViewById(R.id.ListView01);
        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));

        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
               public void onItemClick(AdapterView parentView, View childView, int position, long id) {            

                    if ((position)== 0){

                         Intent intent = new Intent();
                         File file = new File("/sdcard/item1.pdf");

                         if (file.exists()) {

                             Uri path = Uri.fromFile(file);
                             intent.setAction(Intent.ACTION_VIEW);
                             intent.setDataAndType(path, "application/pdf");
                             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                             try {
                                 startActivity(intent);
                             } 
                             catch (ActivityNotFoundException e) {
                                 Toast.makeText(ghcm_Submenu1.this, 
                                     "No Application Available to View PDF", 
                                     Toast.LENGTH_SHORT).show();
                             }
                         }

                    }
                    else if ((position)== 1){

                         Intent intent = new Intent();
                         File file = new File("/sdcard/item2.pdf");

                         if (file.exists()) {

                             Uri path = Uri.fromFile(file);
                             intent.setAction(Intent.ACTION_VIEW);
                             intent.setDataAndType(path, "application/pdf");
                             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                             try {
                                 startActivity(intent);
                             } 
                             catch (ActivityNotFoundException e) {
                                 Toast.makeText(ghcm_Submenu1.this, 
                                     "No Application Available to View PDF", 
                                     Toast.LENGTH_SHORT).show();
                             }
                         }

                    }       
               }
                    public void onNothingSelected(AdapterView parentView) {  

                    }  
                  });   

    }

}