Use webview in Android to open the local file Selector

Source: Internet
Author: User

Button definition on the webpage <input tyoe = "file"/>

Method 1: it is also the most searched solution on the Internet.
Reference: http://stackoverflow.com/questions/4944102/android-webview-file-input-field-filechooser-doesnt-show-up
Http://m0s-programming.blogspot.tw/2011/02/file-upload-in-through-webview-on.html

       @Override      protected void onActivityResult(int requestCode, int resultCode, Intent data) {            if(requestCode==FILECHOOSER_RESULTCODE) {                 if (null == mUploadMessage) return;                          Uri result = data == null || resultCode != RESULT_OK ? null                                  : data.getData();                          mUploadMessage.onReceiveValue(result);                          mUploadMessage = null;                                          }          }

Add the following code to the webchromeclient setting:

        setting.setWebChromeClient(new WebChromeClient() {             // For Android 3.0+         public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {                  if (mUploadMessage != null) return;                mUploadMessage = uploadMsg;                   Intent i = new Intent(Intent.ACTION_GET_CONTENT);                i.addCategory(Intent.CATEGORY_OPENABLE);                i.setType("*/*");                    startActivityForResult( Intent.createChooser( i, "File Chooser" ), BrowserActivity.FILECHOOSER_RESULTCODE );                 }             // For Android < 3.0         public void openFileChooser(ValueCallback<Uri> uploadMsg) {                openFileChooser( uploadMsg, "" );         }           });
// For Android > 4.1.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}

Openfilechooser is a hidden method.
In this way, you can open a directory to upload files. However, in Versions later than 3.0, if the same webpage is opened in the android browser,
There are also functions of photography and recording, and then directly upload the generated image or audio file, so we need to use the following method.
Method 2: refer to the source code program from Com. Android. browser.

   @Override       protected void onActivityResult(int requestCode, int resultCode, Intent data) {            if(requestCode==FILECHOOSER_RESULTCODE) {                 if (null == mUploadMessage) return;                          Uri result = data == null || resultCode != RESULT_OK ? null                                  : data.getData();                            if (result == null && data == null && resultCode == Activity.RESULT_OK) {                            File cameraFile = new File(mCameraFilePath);                            if (cameraFile.exists()) {                                result = Uri.fromFile(cameraFile);                                // Broadcast to the media scanner that we have a new photo                                // so it will be added into the gallery for the user.                                sendBroadcast(                                        new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));                                    }                             }                        mUploadMessage.onReceiveValue(result);                          mUploadMessage = null;              }          }

Add the following code to the webchromeclient setting:

setting.setWebChromeClient(new WebChromeClient() {             // For Android 3.0+         public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {                  if (mUploadMessage != null) return;                mUploadMessage = uploadMsg;                startActivityForResult(createDefaultOpenableIntent(),                        BrowserActivity.FILECHOOSER_RESULTCODE);            }                 }             // For Android < 3.0         public void openFileChooser(ValueCallback<Uri> uploadMsg) {                openFileChooser( uploadMsg, "" );         }  });

private Intent createDefaultOpenableIntent() {           // Create and return a chooser with the default OPENABLE           // actions including the camera, camcorder and sound           // recorder where available.             Intent i = new Intent(Intent.ACTION_GET_CONTENT);             i.addCategory(Intent.CATEGORY_OPENABLE);             i.setType("*/*");             Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(),                    createSoundRecorderIntent());            chooser.putExtra(Intent.EXTRA_INTENT, i);            return chooser;        }                private Intent createChooserIntent(Intent... intents) {            Intent chooser = new Intent(Intent.ACTION_CHOOSER);            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);            chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser");           return chooser;          }    private Intent createCameraIntent() {          Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);          File externalDataDir = Environment.getExternalStoragePublicDirectory(                    Environment.DIRECTORY_DCIM);          File cameraDataDir = new File(externalDataDir.getAbsolutePath() +                    File.separator + "browser-photos");          cameraDataDir.mkdirs();          String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +                    System.currentTimeMillis() + ".jpg";          cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));          return cameraIntent;        }    private Intent createCamcorderIntent() {         return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);        }    private Intent createSoundRecorderIntent() {         return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);        }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.