LoginSignup
26
25

More than 5 years have passed since last update.

androidのwebViewでinput type fileに対応する

Posted at
samplewebview

private final static int FILECHOOSER_RESULTCODE = 1;
private ValueCallback<Uri> mUploadMessage;
private WebView webView = new WebView(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    webview.setWebChromeClient(new WebChromeClient() {
        //androidOS 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String     acceptType,String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult( Intent.createChooser( i, "title" ), FILECHOOSER_RESULTCODE );
        }
        //androidOS 3.0 以上
        public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType){
            openFileChooser( uploadMsg, acceptType ,"" );
         }
        //androidOS 3.0未満
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            openFileChooser( uploadMsg, "","" );
        }
    });
    webView.loadUrl("URL");
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if ( mUploadMessage == null ){
            return;
        }
        Uri result = (intent == null || resultCode != RESULT_OK) ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
}
26
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
25