0

KindleとAndroid用に作成しているアプリにWebサイトからPDFを開きたいです。

私のコードは正しいと思いますが、ファイルが見つからず、技術的にはWebサイトであるため、ファイルを別の形式にする必要があるかどうかを誰かが知っているかどうかわかりませんでした。

任意の入力をいただければ幸いです。これが私がこれまでに持っているものです:

    Button OpenPDF = (Button) findViewById(R.id.button1);
    OpenPDF.setOnClickListener(new View.OnClickListener()
    { 
        public void onClick(View v) 
        {
            File pdfFile = new File("http://thisisthewebsitewithpdf.pdf"); 
            if(pdfFile.exists()) //EXCEPTION HERE. pdfFile doesn't exist!
            {
                Uri path = Uri.fromFile(pdfFile); 
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(PDFActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }
            }
4

1 に答える 1

1

PDFをSDカードにダウンロードし、次のアクティビティを使用して開くことができます。

public class OpenPdf extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.OpenPdfButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File("/sdcard/example.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(OpenPdf.this, 
                            "No Application Available to View PDF", 
                            Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
于 2012-06-19T23:37:39.047 に答える