2011-10-17 72 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(); 
}