2011-07-06 7 views
0

アンドロイドアプリケーションでPDFファイルを表示するためのコードを記述しました。 ボタンをクリックすると特定のページを表示したい。この点で私を助けてください。ボタンをクリックすると、特定のページ番号のapdfファイルに移動する方法

おかげで...

は私がbuttonDep上のクリックが、それは、PDFファイルのページ番号10に行く私が欲しいこの

package com.tiru; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.ActivityNotFoundException; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.PorterDuff; 
import android.graphics.PorterDuffColorFilter; 
import android.graphics.drawable.Drawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SetupForAirport extends ListActivity{ 

    private List<String> item = null; 
    private List<String> path = null; 
    private String root = "/"; 
    private TextView myPath; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

     setContentView(R.layout.setup_airport); 

     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
       R.layout.window_title_for_setupairport); 



     Button buttonDep = (Button) findViewById(R.id.setDep); 
     buttonDep.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       //for Samsung tab 
       myPath = (TextView) findViewById(R.id.path); 
       getDir("/mnt/sdcard/external_sd/doc1/JEPPESEN MANUAL/");  

      } 
     }); 

    } 

    private void getDir(String dirPath) { 
     myPath.setText("Location: " + dirPath); 

     item = new ArrayList<String>(); 
     path = new ArrayList<String>(); 

     File f = new File(dirPath); 
     File[] files = f.listFiles(); 

     if (!dirPath.equals(root)) { 

      item.add(root); 
      path.add(root); 

      item.add("../"); 
      path.add(f.getParent()); 

     } 

     for (int i = 0; i < files.length; i++) { 
      File file = files[i]; 
      path.add(file.getPath()); 
      if (file.isDirectory()) 
       item.add(file.getName() + "/"); 
      else 
       item.add(file.getName()); 
     } 

     ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, 
       R.layout.row, item); 
     setListAdapter(fileList); 
    } 

    protected void onListItemClick(ListView l, View v, int position, long id) { 

     File file = new File(path.get(position)); 

     if (file.isDirectory()) { 
      if (file.canRead()) 
       getDir(path.get(position)); 
      else { 
       new AlertDialog.Builder(this) 
         .setIcon(R.drawable.icon) 
         .setTitle(
           "[" + file.getName() 
             + "] folder can't be read!") 
         .setPositiveButton("OK", 
           new DialogInterface.OnClickListener() { 

            @Override 
            public void onClick(DialogInterface dialog, 
              int which) { 
             // TODO Auto-generated method stub 
            } 
           }).show(); 
      } 
     } else { 

      //For Reading pdf file 
      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(SetupForAirport.this, 
         "No Application Available to View PDF", 
         Toast.LENGTH_SHORT).show(); 
      } 

     } 
    } 

} 

、のようなクラスを作成し

+0

あなたはPDFファイルを開くために別のアプリケーションを使用していますか? – iTurki

+0

@ 2rkいいえ、私は同じアプリケーションで使用しています。私のパスは/ mnt/sdcard/external_sd/doc1/JEPPESEN MANUAL/x.pdfのようなものです –

+0

あなたは 'Intent'をフィルタリングするために' Action'を使っています。その意図。これは私にはあなたの好みのPDFリーダーを選択するオプションをユーザーに与えているようです。私は正しい? – iTurki

答えて

0

することができます別の方法、たとえば:

 Button b; 
    b = (Button)findViewById(R.id.button1); 
     ed = (EditText)findViewById(R.id.editText1); 
     ed.setText("http://static.googleusercontent.com/external_content/untrusted_dlcp/source.android.com/en//compatibility/2.1/android-2.1-cdd.pdf"); 


    b.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String URL = ed.getText().toString(); 
      URL.trim(); 
      System.out.println(URL); 
      WebView web1 =(WebView)findViewById(R.id.webview); 
      web1.getSettings().setJavaScriptEnabled(true); 
      web1.loadUrl("http://docs.google.com/gview?embedded=true&url="+URL);  


     } 
    }); 
+0

疑問がある場合は、私はあなたを助ける準備ができています –

+0

私はurメソッドを試しました、それはウェブのためにうまくいきます。しかし、私は外部のSDカードからpdfファイルを見せたいと思うし、ボタンをクリックすると特定のページを表示したい。そのボタンでは、/ mnt/sdcard/external_sd/doc1/JEPPESEN MANUAL/x.pdf#5のようなページ番号でパスを設定する必要があります。 –

+0

1つだけpdfか複数の表示をしていますか? –