android - Android:如何从 webview 文件选项访问手机上的文件
问题描述
我正在开发一个 webview 应用程序,允许人们从他们的手机上传图像,当我读到 这篇文章时我更加困惑。
我注意到当我点击选择的文件按钮时......它不会打开我的画廊或访问我的文件。
这是我的代码:
package com.benaija.benaija.Fragments;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.benaija.benaija.MainActivity;
import com.benaija.benaija.R;
/**
* Created by Oto-obong on 10/01/2018.
*/
public class HomeFragement extends Fragment {
private WebView webView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
webView = rootView.findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.benaija.com");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setLoadsImagesAutomatically(true);
return rootView;
}
}
请问我该如何解决这个问题?
解决方案
总体而言,您将需要更改 WebView 的代码以允许设置 WebChromeClient,然后实现onFileChooser()方法以使 WebView 能够传达您的设备应在type="file时打开系统的文件资源管理器"输入按钮被按下
我下面的代码打开一个文件选择器,并允许从WebView中选择一个文件以通过 Web 表单上传
MainActivity.java
package com.viatechsystems.test;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
HomeFragment.java
package com.viatechsystems.test;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import static android.app.Activity.RESULT_OK;
public class HomeFragment extends Fragment {
private static final int INPUT_FILE_REQUEST_CODE = 1;
private static final int FILECHOOSER_RESULTCODE = 1;
private static final String TAG = MainActivity.class.getSimpleName();
private WebView webView;
private WebSettings webSettings;
private ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageURI = null;
private ValueCallback<Uri[]> mFilePathCallback;
private String filePath;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}else{
Toast.makeText(getActivity().getApplicationContext(),
"Upload may take as long as you see this message.",
Toast.LENGTH_LONG).show();
}
Uri[] results = null;
//check good response
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
if (filePath != null) {
results = new Uri[]{Uri.parse(filePath)};
}
} else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = data == null ? mCapturedImageURI : data.getData();
}
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(),
"activity :" + e, Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
webView = rootView.findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new ChromeClient()); //set with custom class below
webView.loadUrl("https://files.fm/"); //changed so I could test a button
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setLoadsImagesAutomatically(true);
return rootView;
}
private class ChromeClient extends WebChromeClient {
/*NOTE: method openFileChooser() is different for different Android Versions*/
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
HomeFragment.this.startActivityForResult(
Intent.createChooser(i, "File Selection"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
HomeFragment.this.startActivityForResult(Intent.createChooser(i, "File Selection"),
HomeFragment.FILECHOOSER_RESULTCODE);
}
// For Android 5.0+ --- this is the method that will be called most of the time
public boolean onShowFileChooser(WebView view,
ValueCallback<Uri[]> filePath,
WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePath;
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
Intent[] intentArray = new Intent[0];
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Select Option:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
return true;
}
}
}
fragment_home.xml - 布局资源
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
main_activity.xml - 布局资源
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.viatechsystems.test.HomeFragment"
android:id="@+id/home_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
推荐阅读
- javascript - 静态资源未在 LWC 中加载但在 Aura 中加载成功,为什么?
- mysql - 在 SQL 中执行 MAX(evaluation_expression, return_expression) 的方法
- python-3.x - 我们可以使用 venv 创建特定版本的 python - Python 3.4.3 venv 使用 Python 3.9
- reactjs - 未处理的拒绝(错误):调用恢复异常
- php - Onlinecity SMPP - 添加新的 tlv 参数
- reactjs - 类型“{}”中缺少属性“className”,但类型“IPops”中是必需的
- android - 计算行程距离的解决方案
- excel - Excel 如何向我的 Delphi 应用程序发送通知?
- computer-vision - CNN如何减少参数和重用权重?
- python-3.x - 从 Detectron2 中提取对象特征