zoukankan      html  css  js  c++  java
  • 在Android浏览器中通过WebView调用相机拍照/选择文件 上传到服务器

    最近做的一个项目中,有这样一个要求,在浏览器中调用系统的拍照功能或者选择文件,然后将文件上传到服务器,类似修改头像。        简单而言,就是在一个html页面中有这样一段代码 <input class="filePrew" type="file" capture="camera" accept="image/*" name="image">



    刚开始的时候,没有感觉很难的,因为在UC浏览器、系统自带的浏览器中都可以进行拍照/文件管理器选择,可是在自己所写的Activity中却不行。后来实在是没有思路了,就在网上找了一下,发现要        实现这种功能,都是在webview的WebChromeClient中覆盖掉openFileChooser方法,注意openFileChooser方法在WebChromeClient中有@hide标记。这里只管重写即可,下面将主要代码贴出来,做个记录        

    [java] view plaincopy
    1. private ValueCallback<Uri> mUploadFile;  
    2.     /**拍照/选择文件请求码*/  
    3.     private static final int REQUEST_UPLOAD_FILE_CODE = 12343;  
    4.     private void setWebChromeClient()  
    5.     {  
    6.         if (null != mMainWebView)  
    7.         {  
    8.             mMainWebView.setWebChromeClient(new WebChromeClient()  
    9.             {  
    10.                 // Andorid 4.1+  
    11.                 public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture)  
    12.                 {  
    13.                     openFileChooser(uploadFile);  
    14.                 }  
    15.   
    16.                 // Andorid 3.0 +  
    17.                 public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType)  
    18.                 {  
    19.                     openFileChooser(uploadFile);  
    20.                 }  
    21.   
    22.                 // Android 3.0  
    23.                 public void openFileChooser(ValueCallback<Uri> uploadFile)  
    24.                 {  
    25.                     // Toast.makeText(WebviewActivity.this, "上传文件/图片",Toast.LENGTH_SHORT).show();  
    26.                     mUploadFile = uploadFile;  
    27.                     startActivityForResult(Intent.createChooser(createCameraIntent(), "Image Browser"), REQUEST_UPLOAD_FILE_CODE);  
    28.                 }  
    29.             });  
    30.         }  
    31.     }  
    32.   
    33.     private Intent createCameraIntent()  
    34.     {  
    35.         Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//拍照  
    36.         //=======================================================  
    37.         Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);//选择图片文件  
    38.         imageIntent.setType("image/*");  
    39.         //=======================================================  
    40.         return cameraIntent;  
    41.     }  
    42.     //最后在OnActivityResult中接受返回的结果  
    43.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
    44.     {  
    45.         if (requestCode == REQUEST_UPLOAD_FILE_CODE && resultCode == RESULT_OK)  
    46.         {  
    47.             if (null == mUploadFile)  
    48.             {  
    49.                 return;  
    50.             }  
    51.             Uri result = (null == data) ? null : data.getData();  
    52.             if (null != result)  
    53.             {  
    54.                 ContentResolver resolver = this.getContentResolver();  
    55.                 String[] columns = { MediaStore.Images.Media.DATA };  
    56.                 Cursor cursor = resolver.query(result, columns, nullnullnull);  
    57.                 cursor.moveToFirst();  
    58.                 int columnIndex = cursor.getColumnIndex(columns[0]);  
    59.                 String imgPath = cursor.getString(columnIndex);  
    60.                 System.out.println("imgPath = " + imgPath);  
    61.                 if (null == imgPath)  
    62.                 {  
    63.                     return;  
    64.                 }  
    65.                 File file = new File(imgPath);  
    66.                    //将图片处理成大小符合要求的文件  
    67.                 result = Uri.fromFile(handleFile(file));  
    68.                 mUploadFile.onReceiveValue(result);  
    69.                 mUploadFile = null;       
    70.             }  
    71.         }  
    72.         super.onActivityResult(requestCode, resultCode, data);  
    73.     }  
    74.     /**处理拍照/选择的文件*/  
    75.     private File handleFile(File file)  
    76.     {  
    77.         DisplayMetrics dMetrics = getResources().getDisplayMetrics();  
    78.         BitmapFactory.Options options = new Options();  
    79.         options.inJustDecodeBounds = true;  
    80.          BitmapFactory.decodeFile(file.getAbsolutePath(), options);  
    81.         int imageWidth = options.outWidth;  
    82.         int imageHeight = options.outHeight;  
    83.         System.out.println("  imageWidth = " + imageWidth + " imageHeight = " + imageHeight);  
    84.         int widthSample = (int) (imageWidth / (dMetrics.density * 90));  
    85.         int heightSample = (int) (imageHeight / (dMetrics.density * 90));  
    86.         System.out.println("widthSample = " + widthSample + " heightSample = " + heightSample);  
    87.         options.inSampleSize = widthSample < heightSample ? heightSample : widthSample;  
    88.         options.inJustDecodeBounds = false;  
    89.         Bitmap newBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);  
    90.         System.out.println("newBitmap.size = " + newBitmap.getRowBytes() * newBitmap.getHeight());  
    91.         File handleFile = new File(file.getParentFile(), "upload.png");  
    92.         try  
    93.         {  
    94.             if (newBitmap.compress(CompressFormat.PNG, 50new FileOutputStream(handleFile)))  
    95.             {  
    96.                 System.out.println("保存图片成功");  
    97.             }  
    98.         }  
    99.         catch (FileNotFoundException e)  
    100.         {  
    101.             e.printStackTrace();  
    102.         }  
    103.   
    104.         return handleFile;  
    105.   
    106.     }  


      这样就可以在WebView中上传文件了。记得要添加相应的权限!  
           参考:http://developer.android.com/about/versions/android-3.0.html

      http://blog.sina.com.cn/s/blog_5749ead90101clrn.html  


  • 相关阅读:
    DELL、HP、IBM X86服务器命名规则
    容灾,热备,集群等区别
    HDS(日立)AMS2000系列存储管理配置方法
    KBMMW 4.90.00 发布
    delphi 10 seattle 安卓服务开发(三)
    delphi 10 seattle 安卓服务开发(二)
    delphi 10 seattle 中 解决IOS 9 限制使用HTTP 服务问题
    Android 中的 Service 全面总结(转载)
    KBMMW 4.84.00 发布
    delphi 10 seattle 安卓服务开发(一)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318039.html
Copyright © 2011-2022 走看看