iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >如何解决Android中WebView的input上传照片兼容问题
  • 697
分享到

如何解决Android中WebView的input上传照片兼容问题

androidwebviewinput 2023-05-30 22:05:17 697人浏览 安东尼
摘要

小编给大家分享一下如何解决Android中WEBView的input上传照片兼容问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!问题前几天接到的一个需求,是关于

小编给大家分享一下如何解决AndroidWEBView的input上传照片兼容问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

问题

前几天接到的一个需求,是关于第三方理财产品的H5上传照片问题。

对方说他们的新的需求,需要接入方配合上传资产照片的需求,测试之后发现我们这边的app端,iOS端上传没有问题,而Android端则点击没有任何反应。

对方H5调用的方式是通过<input type='file' accept='image*");   MyBaseWebViewActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR); } // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this public void openFileChooser(ValueCallback uploadMsg, String acceptType) {   selectImage();   mUM = uploadMsg;   Intent i = new Intent(Intent.ACTION_GET_CONTENT);   i.addCateGory(Intent.CATEGORY_OPENABLE);   i.setType("**");   MyBaseWebViewActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MyBaseWebViewActivity.FCR); } //For Android 5.0+ public boolean onShowFileChooser(     WebView webView, ValueCallback<Uri[]> filePathCallback,     WebChromeClient.FileChooserParams fileChooserParams) {   selectImage();   if (mUMA != null) {     mUMA.onReceiveValue(null);   }   mUMA = filePathCallback;   Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   if (takePictureIntent.resolveActivity(MyBaseWebViewActivity.this.getPackageManager()) != null) {     File photoFile = null;     try {       photoFile = createImageFile();       takePictureIntent.putExtra("PhotoPath", mCM);     } catch (IOException ex) {       Log.e(TAG, "Image file creation failed", ex);     }     if (photoFile != null) {       mCM = "file:" + photoFile.getAbsolutePath();       filePath = photoFile.getAbsolutePath();       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));     } else {       takePictureIntent = null;     }   }   Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);   contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);   contentSelectionIntent.setType("*private void selectImage() {  compressPath = Environment.getExternalStorageDirectory().getPath() + "/QWB/temp";  File file = new File(compressPath);  if (!file.exists()) {    file.mkdirs();  }  compressPath = compressPath + File.separator + "compress.png";  File image = new File(compressPath);  if (image.exists()) {    image.delete();  }}// Create an image fileprivate File createImageFile() throws IOException {  @SuppressLint("SimpleDateFORMat") String timeStamp = DateUtils.nowTimeDetail();  String imageFileName = "img_" + timeStamp + "_";  File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);  return File.createTempFile(imageFileName, ".jpg", storageDir);}private String mCM;private String filePath = "";private ValueCallback<Uri> mUM;private ValueCallback<Uri[]> mUMA;private final static int FCR = 1;String compressPath = "";@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent intent) {  super.onActivityResult(requestCode, resultCode, intent);  if (Build.VERSION.SDK_INT >= 21) {    Uri[] results = null;    //Check if response is positive    if (resultCode == Activity.RESULT_OK) {      if (requestCode == FCR) {        if (null == mUMA) {          return;        }        if (intent == null) {          //Capture Photo if no image available          if (mCM != null) {            // results = new Uri[]{Uri.parse(mCM)};            results = new Uri[]{afterChosePic(filePath, compressPath)};          }        } else {          String dataString = intent.getDataString();          if (dataString != null) {            results = new Uri[]{Uri.parse(dataString)};            LogUtil.d("tag", intent.toString());//              String realFilePath = getRealFilePath(Uri.parse(dataString));//              results = new Uri[]{afterChosePic(realFilePath, compressPath)};          }        }      }    }    mUMA.onReceiveValue(results);    mUMA = null;  } else {    if (requestCode == FCR) {      if (null == mUM) return;      Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();      mUM.onReceiveValue(result);      mUM = null;    }  }}private Uri afterChosePic(String oldPath, String newPath) {  File newFile;  try {    newFile = FileUtils.compressFile(oldPath, newPath);  } catch (Exception e) {    e.printStackTrace();    newFile = null;  }  return Uri.fromFile(newFile);}

工具

public class FileUtils {    public static File compressFile(String oldpath, String newPath) {    Bitmap compressBitmap = FileUtils.decodeFile(oldpath);    Bitmap newBitmap = ratingImage(oldpath, compressBitmap);    ByteArrayOutputStream os = new ByteArrayOutputStream();    newBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);    byte[] bytes = os.toByteArray();    File file = null ;    try {      file = FileUtils.getFileFromBytes(bytes, newPath);    } catch (Exception e) {      e.printStackTrace();    }finally{      if(newBitmap != null ){        if(!newBitmap.isRecycled()){          newBitmap.recycle();        }        newBitmap = null;      }      if(compressBitmap != null ){        if(!compressBitmap.isRecycled()){          compressBitmap.recycle();        }        compressBitmap = null;      }    }    return file;  }  private static Bitmap ratingImage(String filePath,Bitmap bitmap){    int degree = readPictureDegree(filePath);    return rotaingImageView(degree, bitmap);  }    public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {    //旋转图片 动作    Matrix matrix = new Matrix();;    matrix.postRotate(angle);    System.out.println("angle2=" + angle);    // 创建新的图片    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,        bitmap.getWidth(), bitmap.getHeight(), matrix, true);    return resizedBitmap;  }    public static int readPictureDegree(String path) {    int degree = 0;    try {      ExifInterface exifInterface = new ExifInterface(path);      int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);      switch (orientation) {        case ExifInterface.ORIENTATION_ROTATE_90:          degree = 90;          break;        case ExifInterface.ORIENTATION_ROTATE_180:          degree = 180;          break;        case ExifInterface.ORIENTATION_ROTATE_270:          degree = 270;          break;      }    } catch (IOException e) {      e.printStackTrace();    }    return degree;  }    public static File getFileFromBytes(byte[] b, String outputFile) {    File ret = null;    BufferedOutputStream stream = null;    try {      ret = new File(outputFile);      FileOutputStream fstream = new FileOutputStream(ret);      stream = new BufferedOutputStream(fstream);      stream.write(b);    } catch (Exception e) {      // log.error("helper:get file from byte process error!");      e.printStackTrace();    } finally {      if (stream != null) {        try {          stream.close();        } catch (IOException e) {          // log.error("helper:get file from byte process error!");          e.printStackTrace();        }      }    }    return ret;  }    public static Bitmap decodeFile(String fPath) {    BitmapFactory.Options opts = new BitmapFactory.Options();    opts.inJustDecodeBounds = true;    opts.inDither = false; // Disable Dithering mode    opts.inPurgeable = true; // Tell to GC that whether it needs free    opts.inInputShareable = true; // Which kind of reference will be used to    BitmapFactory.decodeFile(fPath, opts);    final int REQUIRED_SIZE = 400;    int scale = 1;    if (opts.outHeight > REQUIRED_SIZE || opts.outWidth > REQUIRED_SIZE) {      final int heightRatio = Math.round((float) opts.outHeight          / (float) REQUIRED_SIZE);      final int widthRatio = Math.round((float) opts.outWidth          / (float) REQUIRED_SIZE);      scale = heightRatio < widthRatio ? heightRatio : widthRatio;//    }    Log.i("scale", "scal ="+ scale);    opts.inJustDecodeBounds = false;    opts.inSampleSize = scale;    Bitmap bm = BitmapFactory.decodeFile(fPath, opts).copy(Bitmap.Config.ARGB_8888, false);    return bm;  }     public static void setMkdir(String path)  {    File file = new File(path);    if(!file.exists())    {      file.mkdirs();      Log.e("file", "目录不存在 创建目录  ");    }else{      Log.e("file", "目录存在");    }  }    public static String getFileName(String url)  {    int lastIndexStart = url.lastIndexOf("/");    if(lastIndexStart!=-1)    {      return url.substring(lastIndexStart+1, url.length());    }else{      return null;    }  }    public static void delFile(String path) {    if (!TextUtils.isEmpty(path)) {      File file = new File(path);      if (file.exists()) {        file.delete();      }    }  }}

需要注意的问题

在打release包的时候,因为混淆的问题,点击又会没有反应,这是因为openFileChooser()是系统api,所以需要在混淆是不混淆该方法。

-keepclaSSMembers class * extends android.webkit.WebChromeClient{public void openFileChooser(...);}

当点击拍照之后,如果相机是横屏拍照的话,当拍照结束之后跳回app的时候,会导致app端当前的webView页面销毁并重新打开,需要在androidManifest.xml中当前Activity添加:

android:configChanges="orientation|keyboardHidden|screenSize"

以上是“如何解决Android中WebView的input上传照片兼容问题”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: 如何解决Android中WebView的input上传照片兼容问题

本文链接: https://www.lsjlt.com/news/221612.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何解决Android中WebView的input上传照片兼容问题
    小编给大家分享一下如何解决Android中WebView的input上传照片兼容问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!问题前几天接到的一个需求,是关于...
    99+
    2023-05-30
    android webview input
  • Android 解决WebView无法上传文件的问题
    Android 解决WebView无法上传文件的问题Android原生的WebView并不支持上传文件,需要我们自己实现相应的方法。于是我把工作中的相关代码记录下来。下次直接拿来用就行了。一点一滴都是经验。 1。需要定义三个变量 priva...
    99+
    2023-05-30
    android webview roi
  • css兼容问题如何解决
    这篇文章主要介绍了css兼容问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇css兼容问题如何解决文章都会有所收获,下面我们一起来看看吧。 针对差别的IE浏览器版...
    99+
    2024-04-02
  • 如何解决cssdisplaynlineblock的兼容性问题
    今天就跟大家聊聊有关如何解决cssdisplaynlineblock的兼容性问题,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。 ...
    99+
    2024-04-02
  • win10不兼容问题如何解决
    解决Windows 10不兼容问题可以尝试以下方法:1. 更新驱动程序:访问电脑制造商的官方网站或设备制造商的网站,下载并安装最新的...
    99+
    2023-10-09
    win10
  • 如何解决Vue兼容ie9的问题
    这篇文章给大家分享的是有关如何解决Vue兼容ie9的问题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。前言背景情况vue - 2.5.11vue-cli 使用模板 webpack...
    99+
    2024-04-02
  • 微信小程序中如何解决IOS和Android兼容的问题
    这篇文章将为大家详细讲解有关微信小程序中如何解决IOS和Android兼容的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。微信小程序开发之IOS和Android兼容的...
    99+
    2024-04-02
  • 如何解决在Android中使用setButtonDrawable()方法出现的兼容问题
    这篇文章给大家介绍如何解决在Android中使用setButtonDrawable()方法出现的兼容问题,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Android  setButtonDrawable()的...
    99+
    2023-05-31
    setbuttondrawable() android roi
  • Android如何解决WebView多进程崩溃的问题
    小编给大家分享一下Android如何解决WebView多进程崩溃的问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!问题在android 9.0系统上如果多个进程...
    99+
    2023-06-14
  • JS如何解决position:sticky的兼容性问题
    这篇文章主要介绍JS如何解决position:sticky的兼容性问题,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在项目中有用到sticky的布局,可是由于兼容性问题,在安卓端没有...
    99+
    2024-04-02
  • 如何解决Vue+webpack+Element的兼容问题
    小编给大家分享一下如何解决Vue+webpack+Element的兼容问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!项目中用...
    99+
    2024-04-02
  • Javascript中如何解决浏览器兼容问题
    Javascript中如何解决浏览器兼容问题,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Javascript解决常见浏览器兼...
    99+
    2024-04-02
  • ajax如何实现文件上传成功和解决浏览器兼容问题
    这篇文章主要为大家展示了“ajax如何实现文件上传成功和解决浏览器兼容问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“ajax如何实现文件上传成功和解决浏览器...
    99+
    2024-04-02
  • JavaScript浏览器的兼容问题如何解决
    本文小编为大家详细介绍“JavaScript浏览器的兼容问题如何解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“JavaScript浏览器的兼容问题如何解决”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。inn...
    99+
    2023-07-04
  • 如何解决IE8下不兼容rgba()的问题
    这篇文章主要介绍如何解决IE8下不兼容rgba()的问题,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!rgba()是css3的新属性,所以IE8及以下浏览器不兼容,这怎么办呢?终于我...
    99+
    2024-04-02
  • 如何解决vue-resource promise兼容性问题
    这篇文章主要为大家展示了“如何解决vue-resource promise兼容性问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何解决vue-resourc...
    99+
    2024-04-02
  • 如何解决IE浏览器的兼容问题
    这篇文章主要介绍如何解决IE浏览器的兼容问题,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!怎么用一行代码解决CSS各种IE各种兼容问题<meta http-equiv...
    99+
    2024-04-02
  • 如何解决JS中getElementsByClassName与classList兼容性问题
    这篇文章主要为大家展示了“如何解决JS中getElementsByClassName与classList兼容性问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“...
    99+
    2024-04-02
  • JPA与mybatisplus不兼容问题如何解决
    这篇文章主要介绍了JPA与mybatisplus不兼容问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇JPA与mybatisplus不兼容问题如何解决文章都会有所收获,下面我们一起来看看吧。引入myb...
    99+
    2023-07-05
  • IE7.JS如何解决IE兼容性问题
    小编给大家分享一下IE7.JS如何解决IE兼容性问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!IE7.JS解决IE兼容性问题...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作