如何显示pdf内容?

时间:2011-05-20 09:19:04

标签: android pdf

我想从URL复制pdf文件并将pdf文件内容保存在位图中。 我使用了以下代码,但我没有得到它,因为它是pdf的内容。它给出了不同格式的东西。请帮帮我,告诉我哪里错了。

public class MyActivity extends ListActivity implements OnClickListener
{
 Button addResumeFromUrlBtn;
EditText addResumeFromUrlTxt;
String resume;
@Override
protected void onCreate(Bundle savedInstanceState)
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resumeselection);       

    addResumeFromUrlTxt = (EditText)findViewById(R.id.addResumeFromURLTxt);     
    addResumeFromUrlBtn = (Button)findViewById(R.id.addResumeFromURLBtn);
    addResumeFromUrlBtn.setOnClickListener(this);
}

public String readPDF() throws Exception
{
     BufferedReader in = null;
     String page = "";
     try {
         HttpClient client = new DefaultHttpClient();
         HttpGet request = new HttpGet();
         request.setURI(new URI("http://www.inkwelleditorial.com/pdfSample.pdf"));
         HttpResponse response = client.execute(request);
         in = new BufferedReader
         (new InputStreamReader(response.getEntity().getContent()));
         StringBuffer sb = new StringBuffer("");
         String line = "";
         String NL = System.getProperty("line.separator");
         while ((line = in.readLine()) != null) {
             sb.append(line + NL);
         }
         in.close();
         page = sb.toString();
         } finally {
         if (in != null) {
             try {
                 in.close();
            } catch (IOException e) {e.printStackTrace();}
         }
     }
    return page;        
}

public void onClick(View v) 
{       
    if(v == addResumeFromUrlBtn)
    {   
        try {
            resume = readPDF();             
        } catch (Exception e) 
        {
            e.printStackTrace();
        }   

        RelativeLayout l = (RelativeLayout)findViewById(R.id.resumeRelativelayout);
        TextView txt = new TextView(this);
        txt.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        txt.setDrawingCacheEnabled(true);
         txt.setText(resume);   
        txt.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                  MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        txt.layout(0, 0, txt.getMeasuredWidth(), txt.getMeasuredHeight());          
        txt.setDrawingCacheEnabled(false);           
        l.addView(txt);         
    }
}
}

由于 Monali

2 个答案:

答案 0 :(得分:2)

            File file = new File(fileLocation);    

            if (file.exists()) {
                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);    //Set Intent action view pdf file
                intent.setDataAndType(path, "application/pdf");      //Set data type
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    

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

这是用于显示pdf

答案 1 :(得分:0)

首先在Android中不支持pdf,因此您需要在adob等其他应用中打开,或者如果您想以正确的方式进行操作,那么需要像vudroid和apdfviewer一样制作加载库。

apdfviewer非常好但是没有支持如何编译源代码,实际上所有的lib都在后端使用c ++,所以你需要安装ndk。

Vudroid很慢但你可以轻松编译。

我希望这会对你有所帮助。