在loadUrl()中访问下载目录作为参数

时间:2017-04-19 09:22:39

标签: android

我已经构建了一个小应用程序,用PDF.js渲染pdf文件(例如income_tax_return.pdf)。 PDF文件位于Android项目的assests direcotry以及viewer.html中。现在我正在尝试从我的Andriod设备的下载目录加载PDF并将其作为字符串添加到 loadUrl()方法中的?file属性。

如何在loadUrl()中添加文件属性的路径以呈现PDF?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Use local resource
    Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf");
    Log.i("ABCD",Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf"); //output: /storage/emulated/0/income_tax_return.pdf
     mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=income_tax_return.pdf");
}

修改

用以下代码替换我的代码:

String path = Environment.getExternalStorageDirectory().toString() + "/Download/income_tax_return.pdf";
mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=" + path);

我得到了:

PDF.js Version 1.8.170 (build: 3ca67550 ) Missing PDF "file:///android_asset/psdfjs/web/Download/ income_tax_return.pdf"

3 个答案:

答案 0 :(得分:0)

尝试

String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/income_tax_return.pdf";

mWebView.loadUrl( "file://" + filePath);

how to access downloads folder in android?

答案 1 :(得分:0)

Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf");
  mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=income_tax_return.pdf");

更改为

Sting path = Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf";
mWebView.loadUrl( "file://" + path);

当然,这不在Downloads目录中。但是你明白了。

编辑。你试试吧

mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=Downloads/income_tax_return.pdf");

当该文件位于Downloads目录中时?

答案 2 :(得分:0)

对我有用的是什么:

  try {
        String downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        String path = downloadDirectory + "/income_tax_return.pdf";
        mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + URLEncoder.encode(path, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError("UTF-8 is unknown");
    }

        String downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        String path = downloadDirectory + "/income_tax_return.pdf";
        StringBuffer buffer = new StringBuffer("file:///android_asset/pdfjs/web/viewer.html?");
        buffer.append("file="+ URLEncoder.encode(path));
        mWebView.loadUrl(buffer.toString());