文件上传弹出窗口未打开Mono Android Xamarin [英] File upload popup not opening mono android xamarin

查看:66
本文介绍了文件上传弹出窗口未打开Mono Android Xamarin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​级别为19的xamarin mono.android版本4.我正在尝试使用以下代码在Webview中显示网站,我的网页具有上传文件控件,但使用此应用程序打开时,上传文件在该网页上不起作用在网络视图中.当我在Webview中单击文件上传控件时,没有弹出窗口.

I am using xamarin mono.android Version 4 with api level 19. I am trying below code for showing a website in webview my webpage has a upload file control but upload file is not working on that webpage page when opened using this app in webview. No popup appears when i click on file upload control in webview.

主要活动文件代码

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using webviewUpload;

namespace MyWb
{
    [Activity(Label = "MyWb", MainLauncher = true, Icon = "@drawable/icon")]
    public class MyWb : Activity
    {
        public IValueCallback mUploadMessage;
        public WebView webview;
        public ProgressBar oSpinner;
        public static int FILECHOOSER_RESULTCODE = 1;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            WebView webview = FindViewById<WebView>(Resource.Id.webView1);
            webview.Settings.JavaScriptEnabled = true;
            webview.Settings.AllowFileAccess = true;
            webview.LoadUrl("http://www.script-tutorials.com/demos/199/index.html");
            webview.SetWebViewClient(new WebViewClient());
            webview.SetWebChromeClient(new CustomWebChromeClient(this));

        }
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {

            if (requestCode == FILECHOOSER_RESULTCODE)
            {
                if (null == mUploadMessage) return;
                Android.Net.Uri[] result = data == null || resultCode != Result.Ok ? null : new Android.Net.Uri[] { data.Data };
                try
                {
                    mUploadMessage.OnReceiveValue(result);

                }
                catch (Exception e)
                {
                }

                mUploadMessage = null;
            }
            base.OnActivityResult(requestCode, resultCode, data);
        }
    }

    public class CustomWebChromeClient : WebChromeClient
    {

        MyWb WebViewActivity;
        public CustomWebChromeClient(MyWb activity)
        {
            WebViewActivity = activity;

        }



        public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
        {
            WebViewActivity.mUploadMessage = filePathCallback;
            Intent i = new Intent(Intent.ActionGetContent);
            i.AddCategory(Intent.CategoryOpenable);
            i.SetType("*/*");
            WebViewActivity.StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), MyWb.FILECHOOSER_RESULTCODE);

            return true;
        }


    }

}

主要xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView1"
        android:layout_marginBottom="52.4dp" />
</LinearLayout>

推荐答案

此代码在Github项目链接中正常工作: https://github.com/WaqarSarfaraz/Xamarin-WebView-FileUpload

This Code Works Properly Here id the Github Project Link: https://github.com/WaqarSarfaraz/Xamarin-WebView-FileUpload

public class MyWb : Activity
{
    int count = 1;

    IValueCallback mUploadMessage;
    private static int FILECHOOSER_RESULTCODE = 1;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
        button.Click += delegate {
            button.Text = string.Format ("{0} clicks!", count++);
        };

        var chrome = new FileChooserWebChromeClient ((uploadMsg, acceptType, capture) => {
            mUploadMessage = uploadMsg;
            var i = new Intent (Intent.ActionGetContent);
            i.AddCategory (Intent.CategoryOpenable);
            i.SetType ("image/*");
            StartActivityForResult (Intent.CreateChooser (i, "File Chooser"), FILECHOOSER_RESULTCODE);  
        });

        var webview = this.FindViewById<WebView> (Resource.Id.webView1);
        webview.SetWebViewClient (new WebViewClient ());
        webview.SetWebChromeClient (chrome);
        webview.Settings.JavaScriptEnabled = true;
        webview.LoadUrl ("http://www.script-tutorials.com/demos/199/index.html");
    }

    protected override void OnActivityResult (int requestCode, Result resultCode, Intent intent)
    {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return;
            Java.Lang.Object result = intent == null || resultCode != Result.Ok
                ? null
                : intent.Data;
            mUploadMessage.OnReceiveValue (result);
            mUploadMessage = null;
        }
    }
}

partial class FileChooserWebChromeClient : WebChromeClient
{
    Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;

    public FileChooserWebChromeClient (Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
    {
        this.callback = callback;
    }

    //For Android 4.1
    [Java.Interop.Export]
    public void openFileChooser (IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
    {
        callback (uploadMsg, acceptType, capture);
    }
}

这篇关于文件上传弹出窗口未打开Mono Android Xamarin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆