开发者

Reading a PDF file from Android

I am trying reading a Pdf file from android app.

It builds an app and there is no error, but when I click the button it doesn't do anything. it looks like the app thinks there is no file.

I need a help because i am quite new to android apps but need to finish this job today or tomorrow. The person doing this is away at the moment.

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


your pdf path is incorrect. change other valid path and then try again. then first try whether you set internet permission.


You can't directly fetch a file in a remote server using new File("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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜