I will assist you according to the documentation of Webview. Following image scheme of the visual component WebView
:
Defining variables:
private static final String TAG = TestActivity.class.getSimpleName();
public static final int INPUT_FILE_REQUEST_CODE = 1;
private WebView meuWebView;
private ValueCallback<Uri[]> mFilePathCallback;
private String diretorioFotoImagem;
Defining a method for temporarily storing image armazenarImagemTemp()
:
private File armazenarImagemTemp() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return imageFile;
}
Defining a method to adapt the default settings of WebView
with the name setWebview
:
private void setWebview(WebView webView) {
WebSettings set = webView.getSettings();
// habilita javascript
set.setJavaScriptEnabled(true);
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
// oculta zoom no sdk HONEYCOMB+
set.setDisplayZoomControls(false);
}
// habilita debugging remoto via chrome {inspect}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
meuWebView.setWebViewClient(new WebViewClient());
}
There can only be one method onActivityResult()
in each Activity. This method is called after each call to startActivityForResult
.
When calling the method startActivityForResult
is passed, in the second parameter, a requestCode
. Behold:
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
// verifica se a resposta esta ok com a variável estática {RESULT_OK} do Activity
if(resultCode == Activity.RESULT_OK) {
if(data == null) {
// se não houver dados solicitará que tire uma foto
if(diretorioFotoImagem != null) {
results = new Uri[]{Uri.parse(diretorioFotoImagem)};
}
} else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
return;
}
Ending with the onCreate()
which is the method responsible for loading the layouts
and other start-up operations.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// referencia o webview no layout/activity_main.xml
meuWebView = (WebView) findViewById(R.id.webview);
// define as configurações basicas
setWebview(meuWebView);
if (savedInstanceState != null) {
// retaura o webview
meuWebView.restoreState(savedInstanceState);
}
meuWebView.setWebChromeClient(new WebChromeClient() {
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
if(mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// cria local onde a foto deve ir
File photoFile = null;
try {
photoFile = armazenarImagemTemp();
takePictureIntent.putExtra("PhotoPath", diretorioFotoImagem);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
diretorioFotoImagem = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
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, INPUT_FILE_REQUEST_CODE);
return true;
}
});
// carrega a URL
if(meuWebView.getUrl() == null) {
meuWebView.loadUrl("http://192.168.0.3:8080/api.finan.com/");
}
}
Obs.: do not forget to create the WebView
in his XLM.
To improve the code, check out more details in the documentation.
EDIT
How you pointed out in the comments the question of the possibility to work on Fragment, do the download here of the adaptation basic to work.
you want your website have the possibility to open a method in your android code?
– Icaro
nay. I just want to upload files by input Tyle='file' that has in an html page so by my webview does not work but by cell phone Chrome yes.
– Jasar Orion
The webview javascritp is enabled?
– Icaro
yes ta active. it even opens to select the file but when you select it does not fill in the input
– Jasar Orion
tried switching to this https://github.com/delight-im/Android-AdvancedWebView tbm from the same thing you choose the file and the field is not filled.
– Jasar Orion
It’s been a long time since I’ve had one webview but I remember I had a similar case and needed to import the engine Webkit, with this my webview ran as if it were the Chrome. I don’t know if it’s your solution, but anything you tell me.
– Luiz Augusto Neto
Have you seen the answers to this question in the Soen?
– ramaral
tried tbm use i Webkit from Chrome and did not rolled I will post my Cod here.
– Jasar Orion