Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Uploading Via Webview

$
0
0

Hey Everyone!

I am having trouble uploading a file from a android webview. I have followed all the examples on the web I could find and none of them seem to work properly inside of Xamarin. Natively they do seem to work.

The problem comes with a crash using the IValueCallback after selecting a file in the File Chooser Intent. No matter how I call the IValueCallback it will crash. The file choose does show up properly and pass back a valid path to the selected image for javascript to use.

I followed these tutorials before posting this question:
http://stackoverflow.com/questions/5907369/file-upload-in-webview
http://m0s-programming.blogspot.com/2011/02/file-upload-in-through-webview-on.html (simpler)
http://stackoverflow.com/questions/9376142/mono-for-android-webview-input-field-filechooser-doesnt-work (Mono, this one seemed outdated since there is now a override in webview for OnShowFileChooser and the file chooser is indeed showing)
and a few others.
https://github.com/GoogleChrome/chromium-webview-samples/blob/master/input-file-example/app/src/main/java/inputfilesample/android/chrome/google/com/inputfilesample/MainFragment.java

So here are the errors:
• When I call the callback with the activity result as the above examples show it always says "android.net.uri cannot be converted to android.net.uri[]".
• If I create a list out of the result it says it has already been completed and cannot be called again.
• If I convert it to a string which someone on stack overflow suggested it says it is expecting a android.net.uri.

Here is the code:

`public override bool OnShowFileChooser (WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
if (mActivity == null) { return true; }

        if (mActivity is BaseActivity)
        {
            ((BaseActivity)mActivity).FILECHOOSER_CALLBACK = filePathCallback;
            Intent i = new Intent (Intent.ActionGetContent);
            i.AddCategory (Intent.CategoryOpenable);
            i.SetType ("image/*");
            mActivity.StartActivityForResult ( Intent.CreateChooser (i, "File Chooser"), ((BaseActivity)mActivity).FILECHOOSER_RESULTCODE );
            return false;
        } else {
            return true;
        }
    }`

`protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
if(requestCode == FILECHOOSER_RESULTCODE)
{
if (FILECHOOSER_CALLBACK == null) { return; }
if (resultCode == Result.Ok) {
try{
Android.Net.Uri result = data == null || resultCode != Result.Ok ? null : data.Data;
// throws a "bogey"

                    var a = new Android.Net.Uri[]{ result } ;
                    FILECHOOSER_CALLBACK.OnReceiveValue ( null );

                    // thows a string cannot be converted to android.net.uri[]
                    //FILECHOOSER_CALLBACK.OnReceiveValue ( (string)result  );

                    // throws a android.net.uri cannot be converted to  android.net.uri[]
                    //FILECHOOSER_CALLBACK.OnReceiveValue ( result  );

                    FILECHOOSER_CALLBACK = null;
                }catch(Exception e) {
                    Console.WriteLine (e.Message);
                }
            }
        }
    }`

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>