2011-05-20 151 views
1

我想從URL複製PDF文件並將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

+0

我希望PDF閱讀儘可能簡單 – ingsaurabh 2011-05-20 09:27:34

回答

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

+0

感謝droid,但我必須從URL中讀取PDF不是從文件。你有沒有任何想法如何閱讀網址pdf的內容 – Monali 2011-05-20 10:08:37

0

首先出現在Android的PDF格式不支持,所以你需要一些其他的應用程式,例如adob或者打開你想正確地做到這一點,那麼就需要像vudroid和apdfviewer那樣創建負載庫。

apdfviewer是非常好的,但沒有支持如何編譯源代碼,實際上所有的lib工作在後端的C++,所以你需要安裝ndk。

Vudroid很慢,但您可以輕鬆編譯。

我希望這會幫助你。