android:如何让我的应用程序在点击时打开特定的PDF文件

时间:2010-10-25 04:30:08

标签: android pdf

我想知道我的代码是否可以在Android手机(HTC Desire)中使用现有的PDF查看器查看特定的pdf文件。如果我想从本地文件夹打开pdf文件..我该怎么办?

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) {  

                    }  
                  });   

    }

}

0 个答案:

没有答案