Android Java 在没有 FileChooser 的情况下通过 WebView 上传文件
Android Java Upload File via WebView without FileChooser
我需要编写一个能够通过 WebView 加载网站的 Android 应用程序。该网站包含一个输入 (Type=FILE)
<form action="...">
<input type="file" name="myFile" value="" id="..."><
<input type="submit" value="submit"><
</form>
加载完成后,应用程序应使用特定图像并通过存储路径上传:
String Path = "/storage/emulated/0/myimage.jpg"
我已经尝试打开一个 FileChooser-Dialog 并且它有效,但我需要一个没有文件选择器的解决方案。应该使用 "Path"-Variable 的路径。我知道这将是一个巨大的安全漏洞,但是否有解决方案?也许通过 JavaScript 改变模具输入值?也许有图书馆?
PS:无意做任何违法的事情 - 公司应用程序正在生成个人资料图片,它们需要通过现有的不可更改的文件类型输入上传。
提前致谢。
检查我的回答。您基本上需要做的是覆盖 WebChromeClient
及其方法 openFileChooser
.
class MyWebChromeClient extends WebChromeClient {
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e) {
uploadMessage = null;
Toast.makeText(WebLink.this, "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
}
protected void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
}
你可以试试这个:
1) 获取文件的 URI。
2) 触发器file chooser
3) 在 WebChromeClient
中重写 onShowFileChooser
,像这样:
ValueCallback<Uri[]> mFilePathCallback;
@Override
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
mFilePathCallback.onReceiveValue(myURI);
mFilePathCallback = null;
return true;
}
这里 myURI
是您文件的 URI
。
我需要编写一个能够通过 WebView 加载网站的 Android 应用程序。该网站包含一个输入 (Type=FILE)
<form action="...">
<input type="file" name="myFile" value="" id="..."><
<input type="submit" value="submit"><
</form>
加载完成后,应用程序应使用特定图像并通过存储路径上传:
String Path = "/storage/emulated/0/myimage.jpg"
我已经尝试打开一个 FileChooser-Dialog 并且它有效,但我需要一个没有文件选择器的解决方案。应该使用 "Path"-Variable 的路径。我知道这将是一个巨大的安全漏洞,但是否有解决方案?也许通过 JavaScript 改变模具输入值?也许有图书馆?
PS:无意做任何违法的事情 - 公司应用程序正在生成个人资料图片,它们需要通过现有的不可更改的文件类型输入上传。
提前致谢。
检查我的回答WebChromeClient
及其方法 openFileChooser
.
class MyWebChromeClient extends WebChromeClient {
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e) {
uploadMessage = null;
Toast.makeText(WebLink.this, "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
}
protected void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
}
你可以试试这个:
1) 获取文件的 URI。
2) 触发器file chooser
3) 在 WebChromeClient
中重写 onShowFileChooser
,像这样:
ValueCallback<Uri[]> mFilePathCallback;
@Override
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
mFilePathCallback.onReceiveValue(myURI);
mFilePathCallback = null;
return true;
}
这里 myURI
是您文件的 URI
。