webview 选择文件

网页上的button定义

方法一,也是网上能搜到最多的解决方案
参考网址: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;  
                          
              }
          }

          在WebChromeClient的设定中加入如下代码:

        setting.setWebChromeClient(new WebChromeClient() {
             // For Android 3.0+
         public void openFileChooser(ValueCallback 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 uploadMsg) {
                openFileChooser( uploadMsg, "" );
         }
           });
         // For Android  > 4.1.1
       public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
               openFileChooser(uploadMsg, acceptType);
       }

          openFileChooser为隐藏方法。
           这样可以实现打开目录上传文件。不过在3.0以后的版本中,如果同样的网页在android自带的浏览器打开会发现,
           还有照相,录音的功能,然后直接将生成的图片或音频文件上传,这样就要使用下面的方法。
方法二,参考自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;
              }
          }

          在WebChromeClient的设定中加入如下代码:
    

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

你可能感兴趣的:(android)