【问题标题】:Opening PDF from external URL says "Open File: No apps can perform this action"从外部 URL 打开 PDF 显示“打开文件:没有应用程序可以执行此操作”
【发布时间】:2017-05-22 18:20:36
【问题描述】:

我正在尝试从外部 URL 打开 PDF 文件。代码如下:

private void cargaPdf(Uri url){
    //File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(url,"application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    Intent intent = Intent.createChooser(target, "Open File");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        // Instruct the user to install a PDF reader here, or something
    }
}

我已经安装了 adobe 和 google PDF 查看器,但是当我使用此 url 按下调用“cargaPdf(Uri url)”函数的按钮时(如您所见是一个 pdf 文件):

Uri.parse(https://dl.dropboxusercontent.com/s/oulzgpgnq2ca1ly/KorfbalSpa.pdf?dl=0);

它以西班牙语“Open File Ninguna aplicacion puede realizar esta accion”或英语“Open file No apps can perform this action”出现在屏幕上。会发生什么?

编辑1:好的,现在我知道我必须下载文件才能看到它,所以,我在活动中做了这个代码:

public void cargaDescripcion(View view){
    Log.d("CargaDescripcion", "Antes del directorio");
    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
    File folder = new File(extStorageDirectory, "Mypdf");
    folder.mkdir();
    File file = new File(folder, "Read.pdf");
    try {
        file.createNewFile();
        Log.d("CargaDescripcion", "File creado");
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    Downloader.DownloadFile(deporte.getUrlDescEs(), file);
    Log.d("CargaDescripcion", "Despues de descargarlo");
    showPdf();
    //folder.delete();
}

public void showPdf()
{
    Log.d("showPdf", "Entramos en show pdf");
    File file = new File(Environment.getExternalStorageDirectory()+"/Mypdf/Read.pdf");
    PackageManager packageManager = getPackageManager();
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);
    //file.delete();
}

还有这个类用于下载pdf:

public class Downloader {
public static void DownloadFile(String fileURL, File directory) {
    try {
        FileOutputStream f = new FileOutputStream(directory);
        URL u = new URL(fileURL);
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        InputStream in = c.getInputStream();
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = in.read(buffer)) > 0) {
            f.write(buffer, 0, len1);
        }
        f.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

我从这里得到它Opening pdf file from server using android intent

编辑 2:现在这是下载并显示文件的代码:

public void cargaDescripcion(View view){
    download(deporte.getUrlDescEs());
    showPdf();
    //folder.delete();
}

public void download(String url)
{
    new DownloadFile().execute(url, "read.pdf");
}

public void showPdf()
{
    File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Mypdf/" + "read.pdf");  // -> filename = maven.pdf
    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(SelectedSportActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
    }
}
private class DownloadFile extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... strings) {
        String fileUrl = strings[0];   // -> url del archivo
        String fileName = strings[1];  // -> nombre del archivo.pdf
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extStorageDirectory, "Mypdf");
        folder.mkdir();

        File pdfFile = new File(folder, fileName);

        try{
            pdfFile.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        Downloader.downloadFile(fileUrl, pdfFile);
        return null;
    }
}

但是,文件系统不是由应用程序创建的,并且在应用程序到达下一个点之前没有下载文件,即“显示”。因此,当应用程序查找该文件时,该文件不存在。

编辑 3:解决我在 asynctask 方法的 onPostExecute 上使用的显示问题:

@Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        showPdf();
    }

【问题讨论】:

    标签: android pdf android-intent


    【解决方案1】:

    使用此代码,这对我来说适用于本地文件。

    resumePdfFile文件路径,保存您的文件。

    private void openPDF(String resumePdfFile) {
        //file should contain path of pdf file
    
        Uri path = Uri.fromFile(resumePdfFile);
        Log.e("create pdf uri path==>", "" + path);
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getApplicationContext(),
                    "There is no any PDF Viewer",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    }
    

    您可以通过两种方式查看或下载 pdf 文件,即在设备默认浏览器中打开或通过将其嵌入到您的应用程序中的 web 视图中打开。

    要在浏览器中打开 pdf,

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
    startActivity(browserIntent);
    

    要在网页视图中打开,

    Webview webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(pdf_url);
    

    【讨论】:

    • 您可以通过以下两种方式中的任何一种查看或下载 pdf,即在默认设备浏览器中打开它或通过将其嵌入到您的应用程序中的 web 视图中打开它。查看我编辑的答案。
    • 好的,我已经看到我需要下载苍蝇才能看到它或使用你的两种方式。谢谢,我会试试的。
    • 好的,继续。希望这会帮助你。 :)
    • 我会告诉你:D Thx
    • 您好,现在我可以打开文件,但它们是空的。我已经用代码编辑了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多