Lif*_*ona 5 java android android-studio
我正在尝试多种方法来打开用户的下载文件夹,并允许用户仅选择要打开的 doc 或 pdf 文件。大多数方法都已被弃用,它们的替代方法似乎对我不起作用。
当前代码:
// I've heavily altered the code but hope you guys understand the gist of it.
//Steps:
// Open the Downloads folder
// Let user select the pdf or docx.
// Open the doc or pdf.
// Adding Syllabus Function Method Here.
addSyllabus = findViewById(R.id.add_syllabus_button);
addSyllabus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webview = (WebView) findViewById(R.id.webView);
progressbar = (ProgressBar) findViewById(R.id.progressBar);
webview.getSettings().setJavaScriptEnabled(true);
String filename = "https://s25.q4cdn.com/967830246/files/doc_downloads/test.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
progressbar.setVisibility(View.GONE);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
欣赏,谢谢。
来自DocumentsProvider 的Intent可能是您最好的选择。
来自这个谷歌指南:
private void openFile(Uri pickerInitialUri) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
// Optionally, specify a URI for the file that should appear in the
// system file picker when it loads.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
startActivityForResult(intent, PICK_PDF_FILE);
Run Code Online (Sandbox Code Playgroud)
您可以setType如图所示使用,如果您想默认为“下载”,则可以putExtra如图所示使用。更改pickerInitialUri为正确的 URI,我认为可以将其作为常量找到:Environment.DIRECTORY_DOWNLOADS。
更新:我现在意识到您想要不止一种文件类型。根据此线程:您可以使用:
String [] mimeTypes = {"application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
Run Code Online (Sandbox Code Playgroud)
(我从mozilla获取了 MIME 类型字符串。)
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |