【问题标题】:Browsing for files stopped working in WebView浏览文件在 WebView 中停止工作
【发布时间】:2013-12-14 14:47:10
【问题描述】:

我正在尝试制作一个 android 应用程序来访问我创建的一个简单的 Sinatra 网站。该网站允许用户上传照片。只需通过以下方式使用 WebView,我就可以浏览手机中的文件。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://10.0.2.2:4567");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

当我试图让应用程序变得更好时,我发现这段代码可以让应用程序看起来(并且据说可以工作)更好。

public class MainActivity extends Activity {


    protected WebView myWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        myWebView= (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://10.0.2.2:4567");
        myWebView.setWebViewClient(new NewWebViewClient());

    }

    private class NewWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview,String url)
        {
            webview.loadUrl(url);
            return true;

        }
    }

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
        {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

现在的问题是,当我点击网页中的浏览按钮时,浏览菜单没有出现在我的屏幕上。实际上什么也没发生。我猜 NewWebViewClient 会产生这个问题,但怎么可能呢?有没有办法再次启用文件浏览?我是否必须手动添加它,因为我读到它在早期的 android 版本中是必需的?

我的目标是 API 18,但同样的问题也出现在 API 17 上。

---------------------------------编辑------------- ------------------

仍然有同样的问题。 openFileChooser 为旧 API 提供了解决方案,但不适用于 API 18

    myWebView.setWebChromeClient(new WebChromeClient() 

  {   
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

  mUploadMessage = uploadMsg;  
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
  i.addCategory(Intent.CATEGORY_OPENABLE);  
  i.setType("image/*");  
  Intent chooserIntent = Intent.createChooser(i,"Image Chooser");

  MainActivity16.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);  

         }  
  });  

  setContentView(myWebView);  
 }

【问题讨论】:

  • 我在整个 stackoverflow 中进行了搜索,所有解决方案都建议使用 openFileChooser,但这个解决方案似乎只适用于较旧的 API。API 18 似乎只适用于 WebView,但 WebView 缺少一些功能,如 shouldOverideUrl.There必须是一种方式。阅读苗条的表格会不会有问题?

标签: android file-upload webview webviewclient


【解决方案1】:

https://stackoverflow.com/a/15454070/2394252

毕竟这与 API 有关。 Vorrtex 在上面的链接中给出了解决方案。 就我而言,这是最终代码。

    public class MainActivity extends Activity {

        WebView myWebView ;
        private ValueCallback<Uri> mUploadMessage;
        private final static int FILECHOOSER_RESULTCODE = 1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            myWebView = new WebView(this);
            myWebView.setWebViewClient(new WebViewClient());
            myWebView.setWebChromeClient(new WebChromeClient() {
                //The undocumented magic method override  
                //Eclipse will swear at you if you try to put @Override here  
                public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }

                // For Android > 3.x
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }

                // For Android > 4.1
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }
            });

            this.setContentView(myWebView);

            myWebView.loadUrl("http://10.0.2.2:4567");

        }

        private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
            this.mUploadMessage = uploadMsg;

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");

            this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == FILECHOOSER_RESULTCODE) {
                if (null == this.mUploadMessage) {
                    return;
                }
                Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
                this.mUploadMessage.onReceiveValue(result);
                this.mUploadMessage = null;
            }
        }

        @Override 
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
            {
                myWebView.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
}

很遗憾,由于 openFileChooser 不可用,目前没有针对 kitkat 的解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2014-06-07
    相关资源
    最近更新 更多