SoFunction
Updated on 2025-04-10

File upload instance code in webview support page in Android

Android webview does not support file upload function in web pages by default;

If there is <input type="file" /> in the webpage, a button to browse files will also appear when accessing in the android webview

But there is no response after clicking the button...

So how can the android webview be responsive and this browsing button? I searched a lot of information online, many of which were the same, but all missed a place, which made it impossible to read the full address of the file ("c:\upfile\"). I sorted out the final code and entered:

We need to set up WebChromeClient for webview and overwrite the file selection method in the implementation class of WebChromeClient:

package ; 
 
import ; 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity { 
  private ValueCallback&lt;Uri&gt; mUploadMessage; 
  private final static int FILECHOOSER_RESULTCODE = 1; 
  private WebView web; 
  private ProgressBar progressBar; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_main); 
 
    web = (WebView) findViewById(.webView1); 
    progressBar = (ProgressBar) findViewById(.progressBar1); 
 
    web = new WebView(this); 
    ().setJavaScriptEnabled(true); 
    ("/website/"); 
    (new myWebClient()); 
    (new WebChromeClient() { 
      // The undocumented magic method override 
      // Eclipse will swear at you if you try to put @Override here 
      // For Android 3.0+ 
      public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg) { 
 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        (Intent.CATEGORY_OPENABLE); 
        ("image/*"); 
        ( 
            (i, "File Chooser"), 
            FILECHOOSER_RESULTCODE); 
 
      } 
 
      // For Android 3.0+ 
      public void openFileChooser(ValueCallback uploadMsg, 
          String acceptType) { 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        (Intent.CATEGORY_OPENABLE); 
        ("*/*"); 
        ( 
            (i, "File Browser"), 
            FILECHOOSER_RESULTCODE); 
      } 
 
      // For Android 4.1 
      public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, 
          String acceptType, String capture) { 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        (Intent.CATEGORY_OPENABLE); 
        ("image/*");
         (
             (i, "File Chooser"),
             MainActivity.FILECHOOSER_RESULTCODE);
 
       }
 
     });
 
     setContentView(web);
   }
 
   @Override
   protected void onActivityResult(int requestCode, int resultCode,
       Intent intent) {
     if (requestCode == FILECHOOSER_RESULTCODE) {
       if (null == mUploadMessage)
         return;
       Uri result = intent == null || resultCode != RESULT_OK ? null
           : ();
       
       // (result);
       // mUploadMessage = null;
       Bitmap bm = null;
       //Outsiders can access the data provided by ContentProvider through the ContentResolver interface
       ContentResolver resolver = getContentResolver();
       try {
         Uri originalUri = (); // Obtain the uri of the picture
         bm = (resolver, originalUri);
         // The second part that starts here is to get the path to the picture:
         String[] proj = { };
         // It seems to be the encapsulation interface of the Android multimedia database. For details, please refer to the Android documentation.
         Cursor cursor = managedQuery(originalUri, proj, null, null,
             null);
         // According to my personal understanding, this is to obtain the index value of the image selected by the user.
         int column_index = cursor
             .getColumnIndexOrThrow();
         // Move the cursor to the beginning, this is very important, and it can easily cause cross-border
         ();
         // Finally, get the image path based on the index value
 
         String path = (column_index);
         Uri uri = (new File(path));
         (uri);
       } catch (IOException e) {
 
         ("TAG-->Error", ());
 
       }
     }
   }
 
   public class myWebClient extends WebViewClient {
     @Override
     public void onPageStarted(WebView view, String url, Bitmap favicon) {
       // TODO Auto-generated method stub
       (view, url, favicon);
     }
 
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
       // TODO Auto-generated method stub
 
       (url);
       return true;
 
     }
 
     @Override
     public void onPageFinished(WebView view, String url) {
       // TODO Auto-generated method stub
       (view, url);
 
       ();
     }
   }
 
   // flipscreen not loading again
   @Override
   public void onConfigurationChanged(Configuration newConfig) {
     (newConfig);
   }
 
   // To handle "Back" key press event for WebView to go back to previous
   // screen.
   /*
    * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if
    * ((keyCode == KeyEvent.KEYCODE_BACK) && ()) { ();
    * return true; } return (keyCode, event); }
    */ 
} 


The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.