1

I want to copy the pdf file from URL to and save the pdf file content in bitmap. I have used following code but I am not getting the as it is content of pdf. It is giving something in different format.Please help me and tell me where I am wrong.

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);         
    }
}
}

Thanks Monali

Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
Monali
  • 1,966
  • 12
  • 39
  • 59

2 Answers2

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();
                }
            }

This is the for display pdf

Andy
  • 5,379
  • 7
  • 39
  • 53
  • thanks droid but i have to read the pdf from url not from file. Do u have any idea how to read content of pdf from url – Monali May 20 '11 at 10:08
0

First of there is no support for pdf in Android so you need to open in some other app like adob or if you want to do it right way then need make the load lib like vudroid and apdfviewer .

apdfviewer is very good but there is no support how to compile source code, actually all lib work with c++ in backend so you need to install the ndk.

Vudroid is slow but you can compile easily.

I hope this will help you.

Sameer Z.
  • 3,265
  • 9
  • 48
  • 72