![](/img/trans.png)
[英]Unable to pick file (pdf, doc, docx) from download folder in Android 11 and above
[英]Getting pdf or doc file from Downloads Folder in Android
我正在嘗試多種方法來打開用戶的下載文件夾,並允許用戶僅打開 select 文檔或 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);
}
});
}
});
欣賞它,謝謝。
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);
您可以如圖所示使用setType
,如果您想默認為 Downloads,您可以使用如圖所示的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);
(我從mozilla獲得了 MIME 類型字符串。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.