我正在开发一个Webview应用程序,最终使文件选择器(从输入文件形式)起作用,但问题是只能起作用一次。
老实说,我真的是Android Studio的新手,最终使这段代码由我自己来实现,机器人,问题是输入文件的WebView文件选择器只能运行一次
希望您能帮我解释一下,要使它生效,我还需要做些什么,这是我的Mainactivity.java
package www.tribbr.tribbr;
import androidx.appcompat.app.AppCompatactivity;
import androidx.core.app.activityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeclient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Mainactivity extends AppCompatactivity {
private final static int FCR = 1;
private WebView webView;
private ValueCallback<Uri> mUM;
private ValueCallback<Uri[]> mUMA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
activityCompat.requestPermissions(Mainactivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA},1);
}
assert webView != null;
webView=findViewById(R.id.webviewid);
webView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new MyAppWebViewClient());
WebSettings WebSettings = webView.getSettings();
WebSettings.setJavaScriptEnabled(true);
WebSettings.setallowFileaccess(true);
webView.getSettings().setSupportzoom(true);
webView.getSettings().setBuiltInzoomControls(true);
if (Build.VERSION.SDK_INT >= 21) {
WebSettings.setMixedContentMode(0);
webView.setLayerType(View.LAYER_TYPE_HARDWARE,null);
} else if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE,null);
} else if (Build.VERSION.SDK_INT < 19) {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
}
webView.setWebChromeclient(new WebChromeclient(){//For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUM = uploadMsg;
Intent i = new Intent(Intent.actION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Mainactivity.this.startactivityForResult(Intent.createChooser(i,"File Chooser"),FCR);
}
// For Android 3.0+,above method not supported in some android 3+ versions,in such case we use this
public void openFileChooser(ValueCallback uploadMsg,String acceptType) {
mUM = uploadMsg;
Intent i = new Intent(Intent.actION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Mainactivity.this.startactivityForResult(
Intent.createChooser(i,"File Browser"),FCR);
}
//For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType,String capture) {
mUM = uploadMsg;
Intent i = new Intent(Intent.actION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Mainactivity.this.startactivityForResult(Intent.createChooser(i,Mainactivity.FCR);
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView,ValueCallback<Uri[]> filePathCallback,WebChromeclient.FileChooserParams fileChooserParams) {
if (mUMA != null) {
mUMA.onReceiveValue(null);
}
mUMA = filePathCallback;
// Intent takePictureIntent = new Intent(MediaStore.actION_IMAGE_CAPTURE);
Intent contentSelectionIntent = new Intent(Intent.actION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
// Intent[] intentArray;
//
// if (takePictureIntent != null) {
// intentArray = new Intent[]{takePictureIntent};
// } else {
// intentArray = new Intent[0];
// }
Intent chooserIntent = new Intent(Intent.actION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT,contentSelectionIntent);
// chooserIntent.putExtra(Intent.EXTRA_TITLE,"Image Chooser");
// chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,intentArray);
startactivityForResult(chooserIntent,FCR);
return true;
}
});
webView.loadUrl("https://tribbr.me/login.php");
}
@Override
public void onBackpressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackpressed();
}
}
}
希望此输入文件选择器可以多次工作。...非常感谢:)