在我的应用程序中打开并查看pdf[英] Open and view pdf in my application

本文是小编为大家收集整理的关于在我的应用程序中打开并查看pdf的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

使用此代码:

try {

    URL url = new URL(aurl[0]);
    URLConnection conexion = url.openConnection();
    conexion.connect();

    int lenghtOfFile = conexion.getContentLength();
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

    InputStream input = new BufferedInputStream(url.openStream());
    FileOutputStream fos = openFileOutput("try.pdf", Context.MODE_PRIVATE);
    //PARA DESCARGARLO EN SD//
//  OutputStream output = new FileOutputStream("/sdcard/prueba.pdf");

    byte data[] = new byte[1024];

    long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            fos.write(data, 0, count);
        }

        fos.flush();
        fos.close();
        input.close();
    } catch (Exception e) {}
    return null;

我在申请中保持8 MB大小的PDF.我用了几个代码来试图打开它并查看它,但找不到方式.

任何建议?

推荐答案

请参阅下面,因此使用pdfviewer从sdcard读取pdf的答案链接.

读取pdf文件

其他推荐答案

嗨,请尝试以下代码.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("mnt/sdcard/try.pdf")), "application/pdf");
try 
{
    startActivity(intent);
} 
catch (ActivityNotFoundException e) 
{
    // Toast Message
}

这里sFile是 sdcard路径.

本文地址:https://www.itbaoku.cn/post/102326.html

问题描述

Using this code:

try {

    URL url = new URL(aurl[0]);
    URLConnection conexion = url.openConnection();
    conexion.connect();

    int lenghtOfFile = conexion.getContentLength();
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

    InputStream input = new BufferedInputStream(url.openStream());
    FileOutputStream fos = openFileOutput("try.pdf", Context.MODE_PRIVATE);
    //PARA DESCARGARLO EN SD//
//  OutputStream output = new FileOutputStream("/sdcard/prueba.pdf");

    byte data[] = new byte[1024];

    long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            fos.write(data, 0, count);
        }

        fos.flush();
        fos.close();
        input.close();
    } catch (Exception e) {}
    return null;

I keep in the application a PDF of 8 MB size. I've used several codes to try to open it and view it but can not find the way.

Any suggestions?

推荐答案

Please see below SO Answer's link for read pdf from sdcard using PDFViewer.

Read PDF Files

其他推荐答案

Hi Please try below code.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("mnt/sdcard/try.pdf")), "application/pdf");
try 
{
    startActivity(intent);
} 
catch (ActivityNotFoundException e) 
{
    // Toast Message
}

Here sFile is SDcard Path.