如何上传我的WebView中的多个图像? [英] How do I upload multiple images in my WebView?

查看:256
本文介绍了如何上传我的WebView中的多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在我的Android应用程序中使WebView允许上传多个文件使用相机拍摄的图像。



当我在浏览器应用程序中打开下面的HTML代码时,我可以附加多张图片。
当我的应用程序中的WebView中有相同的代码时,该按钮甚至不会打开对话框。

 < form method =postaction =/ save / imagesname =sendid =sendenctype =multipart / form-data> 
< input type =filename =data []id =cameramultiple =>
< input type =submitname =send>
< / form>

如果您想要试试这个链接,可以使用以下链接: http://codereaper.com/bugspray/so/25251993/html-sendimages/



这里的关键是Android应用程序本身有什么都不做这样做,加载的URL包含一个网页,使其在Android的浏览器应用程序,您可以使用相机上传一堆图片。

我的尝试的当前状态包括给应用程序的权限:

 < uses-feature android:name =android.hardware.camera/> 
<使用权限android:name =android.permission.CAMERA/>
<使用权限android:name =android.permission.FLASHLIGHT/>

创建WebView时允许使用JavaScript和FileAcess:

  protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);

WebView webView =(WebView)findViewById(R.id.report_webview);

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings()。setAllowFileAccess(true);

showProgressDialog(R.string.please_wait_title,R.string.please_wait_description);

webView.setWebViewClient(new WebViewClient(){
@Override $ b $ public void onPageFinished(WebView view,String url){
super.onPageFinished(view,url) ;
closeProgressDialog();
}
}
);

webView.loadUrl(http://codereaper.com/bugspray/so/25251993/html-sendimages/);



$ b $ p
$ b在我的搜索中,我发现很多引用使用一个未记录的特性' openFileChooser':

  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 / *);
Cv5appActivity.this.startActivityForResult(
Intent.createChooser(i,图像浏览器),
FILECHOOSER_RESULTCODE);




$ b我只支持较新的Android版本(4.3及以上版本),并希望以支持和记录的方式做到这一点,但如果没有其他方法存在,将使用此方法。然而,我不明白'openFileChooser'方法将如何:
$ b $ ul
允许多个图像被拍摄/上传
  • 将捕获的图像传回到网页上的/ ajax / html表格



  • WebView的行为就像Android上的浏览器应用程序,或者使WebChromeClient能够处理多个图像,并将它们交还给WebView。



    干杯

    $ b

    解决方案

    解决方案我解决了所有的问题,而使用其他答案,涉及'openFileChooser'方法。我需要发现/理解的关键信息是 ValueCallback< Uri> ,这既是将所选图像传回的方式,也是处理多重图片到WebView。



    在执行openFileChooser方法时,您交给 ValueCallback< Uri> 然后负责调用它的回调与选定的图像交还,或者返回null:

      mUploadMessage.onReceiveValue(null); 

    另外请注意,如果您不调用回调,您的WebView将停止正常工作。 >

    更新:



    最近这个问题再次出现,所以现在我正在使用 https://github.com/delight-im/Android-AdvancedWebView - 到目前为止它正在工作精美我不必编码。


    How can I make the file upload in my app's WebView behave like in the browser app?

    I am trying to make a WebView in my Android application allow uploading multiple images taken with the camera.

    When I have the HTML code below open in the browser app, I can attach multiple pictures. When I have the same code in my WebView in my app, the button won't even open a dialog.

    <form method="post" action="/save/images" name="send" id="send" enctype="multipart/form-data">
       <input type="file" name="data[]" id="camera" multiple="">
       <input type="submit" name="send">
    </form>
    

    Here's a link to the above HTML if you want to try it out: http://codereaper.com/bugspray/so/25251993/html-sendimages/

    The point here is the Android application itself have nothing to do with this, the loaded URL contain a webpage made to function in Android's browser application where you can use the camera to upload a bunch of images.

    The current state of my attempts consists of giving permissions to the app:

    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    

    Allowing JavaScript and FileAcess when creating the WebView:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
    
        WebView webView = (WebView) findViewById(R.id.report_webview);
    
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setAllowFileAccess(true);
    
        showProgressDialog(R.string.please_wait_title, R.string.please_wait_description);
    
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                closeProgressDialog();
            }
        }
        );
    
        webView.loadUrl("http://codereaper.com/bugspray/so/25251993/html-sendimages/");
    }
    

    In my searches I have found a lot of references to use an undocumented feature 'openFileChooser':

       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/*");
                Cv5appActivity.this.startActivityForResult(
                        Intent.createChooser(i, "Image Browser"),
                        FILECHOOSER_RESULTCODE);
            }
        }
    

    I am supporting only newer Android versions (4.3 and above) and would like to do this in supported and documented way, but will use this method if no other method exist. However I do not see how the 'openFileChooser' method will:

    • allow multiple images to taken/uploaded
    • hand the images captured back to the form/ajax/html on the webpage

    Any advice would be appreciated in order to make the WebView act like the browser app on Android or making WebChromeClient able to handle multiple images and "hand them back to the WebView".

    Cheers

    解决方案

    I did solve all my problems a while back using the other answers on SO that involves the 'openFileChooser' methods. The key piece of information I needed to discover/understand was the ValueCallback<Uri> which is both the way of handing the selected images back, but also puts the responsibility of handling multiple images to the WebView.

    You are handed a ValueCallback<Uri> while implementing the 'openFileChooser' methods and you are then responsible for calling its callback to hand back with the selected image or null, like:

    mUploadMessage.onReceiveValue(null);
    

    Also note that if you do not call the callback, your WebView will stop working right.

    UPDATE:

    Recently the issue reappeared so now I am using a fork of https://github.com/delight-im/Android-AdvancedWebView - so far it is working beautifully and I didn't have to code it.

    这篇关于如何上传我的WebView中的多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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