2013-12-14 22 views
1

我想让一个android应用程序访问我创建的一个简单的Sinatra网站。该网站允许用户上传照片。通过以下方式使用WebView,可以浏览手机中的文件。浏览在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); 
} 
+0

找遍整个计算器和所有的解决方案建议使用openFileChooser,但这种解决方案似乎只与旧的API工作。 API 18似乎只适用于WebView,但WebView缺少像shouldOverideUrl这样的几个功能。必须有一种方法。 这可能是阅读苗条形式的问题吗? –

回答

0

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; 
     } 
} 

不幸的是,在目前的kitkat无解,因为openFileChooser不可