Я пишу гибридное приложение и хочу использовать загрузку файлов со страницы HTML (файл и камера).Я нашел много кода, и я использую его ниже (и работаю хорошо)
Когда пользователь нажимает на вход FileUploader, открывается собственное окно (для выбора между файлом или камерой), и все будетОК:
Моя единственная проблема - когда пользователь нажимает из окна, это будет закрыто и после того, как больше не будет открыто, может быть что-то для повторной инициализации или перезапуска ...
если у кого-то есть идеи ...
спасибо
На MainActivity:
var chrome = new SmarterWebChromeClient((uploadMsg) =>
{
mUploadMessage = uploadMsg;
mCameraPhotoPath = null;
Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
//Create the File where the photo should go
File photoFile = null;
try
{
photoFile = createImageFile();
takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
}
catch (IOException ex)
{
// Error occurred while creating the File
writeEx(ex.ToString());
}
// Continue only if the File was successfully created
if (photoFile != null)
{
mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput,
Android.Net.Uri.FromFile(photoFile));
}
else
{
takePictureIntent = null;
}
Intent contentSelectionIntent = new Intent(Intent.ActionGetContent);
contentSelectionIntent.AddCategory(Intent.CategoryOpenable);
contentSelectionIntent.SetType("image/*");
Intent[] intentArray;
if (takePictureIntent != null)
{
intentArray = new Intent[] { takePictureIntent };
}
else
{
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ActionChooser);
chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
chooserIntent.PutExtra(Intent.ExtraTitle, "Choisir une photo");
chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);
StartActivityForResult(chooserIntent, MainActivity.FILECHOOSER_RESULTCODE);
});
mWebView.SetWebViewClient(new MyWebViewClient());
mWebView.SetWebChromeClient(chrome);
...
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage)
return;
// Check that the response is a good one
if (resultCode == Result.Ok)
{
Android.Net.Uri[] results = null;
if (intent == null)
{
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(mCameraPhotoPath) };
}
else
{
}
}
else
{
if (intent.DataString != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(intent.DataString) };
}
}
mUploadMessage.OnReceiveValue(results);
mUploadMessage = null;
}
}
}
И мой класс WebChromeClient:
partial class SmarterWebChromeClient : WebChromeClient
{
Action<IValueCallback> callback;
public SmarterWebChromeClient(Action<IValueCallback> callback)
{
this.callback = callback;
}
public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
callback(filePathCallback);
return true;
}
}