2011-10-17 109 views
0

我正在嘗試從android應用中讀取PDF文件。從Android中讀取PDF文件

它構建一個應用程序,並沒有錯誤,但是當我點擊按鈕它什麼也不做。它看起來應用程序認爲沒有文件。

我需要幫助,因爲我對android應用程序很陌生,但需要今天或明天完成這項工作。這樣做的人目前不在。

package com.readPDF; 

    import java.io.File; 

    import android.app.Activity; 
    import android.content.ActivityNotFoundException; 
    import android.content.Context; 
    import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

    public class ReadPDF extends Activity { 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button button = (Button) findViewById(R.id.pdfbutton); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getContext(), "in.", Toast.LENGTH_LONG).show(); 
      File file = new File("http://path/pathtopdf/mypdf.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(ReadPDF.this, 
         "No Application Available to View PDF", 
         Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 

     private Context getContext() { 
      // TODO Auto-generated method stub 
      return null; 
     } 
    }); 
    } 
    } 

回答

0

您的pdf路徑不正確。更改其他有效路徑,然後重試。那麼首先嚐試是否設置了互聯網許可。

+0

是的,我做了一個正確的道路。互聯網許可已設置。我只是改變了路徑,因爲我不想讓每個人都看到真正的道路。 – user973067 2011-10-17 14:15:54

0

您無法使用新文件(「http url」)直接在遠程服務器中獲取文件。

URI uri = new URI("http", "//path/pathtopdf/mypdf.pdf", null); 
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(ReadPDF.this, 
         "No Application Available to View PDF", 
         Toast.LENGTH_SHORT).show(); 
}