title: How to upload file from fragment webview in android?
body: <p>I have used tabhost of two fragments. One fragment is for list and other is for webview. From that fragment I have to upload file from gallery. Following is my fragment for webview.</p>
  
  <pre><code>public class AppleFragment extends Fragment{
  
  private static final String TAG = "SocialActivityFragment";
  //protected static final int FILECHOOSER_RESULTCODE = 0;
  WebView browser;
  static String cookieString;
  ProgressBar Pbar;
  HashMap&lt;String, String&gt; headerMap = new HashMap&lt;String, String&gt;();
   private ValueCallback&lt;Uri&gt; mUploadMessage;  
   final static int FILECHOOSER_RESULTCODE=1;  
  
   @Override
  public void onActivityResult(int requestCode, int resultCode,Intent intent) {  
    if(requestCode==FILECHOOSER_RESULTCODE)  
    {  
     if (null == mUploadMessage) return;  
              Uri result = intent == null || resultCode != Activity.RESULT_OK ? null  
                      : intent.getData();  
              mUploadMessage.onReceiveValue(result);  
              mUploadMessage = null;  
    }
    }  
  @SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      /** Creating array adapter to set data in listview */
      //ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity().getBaseContext(), R.layout.social_activities_layout, apple_versions);
      LayoutInflater layoutInflater = (LayoutInflater) getActivity()
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View layout = layoutInflater.inflate(R.layout.social_layout, null);
      Pbar = (ProgressBar) layout.findViewById(R.id.pB1);
      String strResponse = util.makeWebCall("http://google.com");
  
      DefaultHttpClient mClient = util.getClient();
  
  
      Cookie sessionInfo;
      List&lt;Cookie&gt; cookies = mClient.getCookieStore().getCookies();
      if (! cookies.isEmpty()){
          CookieSyncManager.createInstance(getActivity().getApplicationContext());
          CookieManager cookieManager = CookieManager.getInstance();
  
          for(Cookie cookie : cookies){
                  sessionInfo = cookie;
                  cookieString = sessionInfo.getName() + "=" + Sessions.getPhpSessid(getActivity().getApplicationContext()) + "; domain=" + sessionInfo.getDomain();
                  cookieManager.setCookie("http://youornot.com/social/", cookieString);
                  CookieSyncManager.getInstance().sync();
                  //Toast.makeText(getApplicationContext(), cookieString, Toast.LENGTH_LONG).show();
          }
  }
  
  AlertDialogManager alert=new AlertDialogManager();
  ConnectionDetector cd=new ConnectionDetector(getActivity().getApplicationContext());
  if(!cd.isConnectedToInternet())
  {
      alert.showAlertDialog(getActivity(), "Error..!", "You have not Connected to Internet", true);
  }
  
  browser=(WebView)layout.findViewById(R.id.webBrowser);
    // WebView browser = new WebView(SocialActivity.this);
  
  WebSettings settings = browser.getSettings();
  
  browser.setScrollBarStyle(browser.OVER_SCROLL_IF_CONTENT_SCROLLS);
  
  WebViewClient wvClient = new WebViewClient();
  browser.setWebViewClient(wvClient);
  browser.getSettings().setJavaScriptEnabled(true);
  browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  
  
  browser.setWebChromeClient(new WebChromeClient() {
  
       @SuppressWarnings("unused")
      public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg) {  
  
           mUploadMessage = uploadMsg;  
           Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
           i.addCategory(Intent.CATEGORY_OPENABLE);  
           i.setType("*/*");  
           getActivity().startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  
  
          }
  
       // For Android 3.0+
          @SuppressWarnings("unused")
          public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
          mUploadMessage = uploadMsg;
          Intent i = new Intent(Intent.ACTION_GET_CONTENT);
          i.addCategory(Intent.CATEGORY_OPENABLE);
          i.setType("*/*");
          getActivity().startActivityForResult(
          Intent.createChooser(i, "File Browser"),
          FILECHOOSER_RESULTCODE);
          }
  
       //For Android 4.1
          @SuppressWarnings("unused")
          public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String acceptType, String capture){
              mUploadMessage = uploadMsg;  
              Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
              i.addCategory(Intent.CATEGORY_OPENABLE);  
              i.setType("*/*");  
              getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ),FILECHOOSER_RESULTCODE );
  
          }
  
      public void onProgressChanged(WebView view, int progress) 
         {
         if(progress &lt; 100 &amp;&amp; Pbar.getVisibility() == ProgressBar.GONE){
             Pbar.setVisibility(ProgressBar.VISIBLE);
             //txtview.setVisibility(View.VISIBLE);
         }
         Pbar.setProgress(progress);
         if(progress == 100) {
             Pbar.setVisibility(ProgressBar.GONE);
            // txtview.setVisibility(View.GONE);
         }
      }
  });
  
  headerMap.put("Cookie", cookieString);
  browser.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
  
          switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
              case MotionEvent.ACTION_UP:
                  //The key is this line. 
                  v.requestFocusFromTouch();  
                  break;
          }               
          return false;
      }
  });
  browser.loadUrl("http://google.com", headerMap);
  
  
      return layout;//super.onCreateView(inflater, container, savedInstanceState);
  }
  
  @Override
  public void onStart() {
          super.onStart();
  
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
      getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  }
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      // TODO Auto-generated method stub
      super.onCreateOptionsMenu(menu, inflater);
      inflater.inflate(R.menu.web_menu, menu);
  }
  
  @SuppressLint("NewApi")
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
     // handle item selection
     switch (item.getItemId()) {
        case R.id.refresh:
            browser.reload();
           // browser.loadUrl("http://google.com", headerMap);
           return true;
        default:
           return super.onOptionsItemSelected(item);
     }
  }   
  }
  </code></pre>
  
  <p>Whenever I am uploading file then webview is not responding at all.When i will close the application then also webview not responding. So how I can upload file in webview that is in the tabhost fragment.</p>
  
  <p>Thanks in advance.</p>