欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android WebView的input上傳照片的兼容問題

 更新時間:2017年08月15日 11:08:22   作者:指間沙似流年  
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問題,非常具有實用價值,需要的朋友可以參考下

問題

前幾天接到的一個需求,是關(guān)于第三方理財產(chǎn)品的H5上傳照片問題。

對方說他們的新的需求,需要接入方配合上傳資產(chǎn)照片的需求,測試之后發(fā)現(xiàn)我們這邊的app端,IOS端上傳沒有問題,而Android端則點擊沒有任何反應(yīng)。

對方H5調(diào)用的方式是通過<input type='file' accept='image/*'/>的方式調(diào)用,本來以為這個問題很簡單,就是app端沒有設(shè)置相機權(quán)限,造成的點擊無反應(yīng)情況,而實際上加了之后發(fā)現(xiàn),并非簡單的權(quán)限問題。

解決問題

因為Android的版本碎片問題,很多版本的WebView都對喚起函數(shù)有不同的支持。

我們需要重寫WebChromeClient下的openFileChooser()(5.0及以上系統(tǒng)回調(diào)onShowFileChooser())。我們通過Intent在openFileChooser()中喚起系統(tǒng)相機和支持Intent的相關(guān)app。

在系統(tǒng)相機或者相關(guān)app中一頓操作之后,當(dāng)返回app的時候,我們在onActivityResult()中將選擇好的圖片通過ValueCallback的onReceiveValue方法返回給WebView。

附上代碼:

1、首先是重寫各個版本的WebChromeClient的支持

webView.setWebChromeClient(new WebChromeClient() {
 //For Android 3.0+
 public void openFileChooser(ValueCallback<Uri> uploadMsg) {
   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"), 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 Browser"),
       FCR);
 }

 //For Android 4.1+
 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
   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("*/*");
   Intent[] intentArray;
   if (takePictureIntent != null) {
     intentArray = new Intent[]{takePictureIntent};
   } else {
     intentArray = new Intent[0];
   }

   Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
   chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
   chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
   chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
   startActivityForResult(chooserIntent, FCR);
   return true;
 }
});

2、選完照片之后

/**
* 打開圖庫,同時處理圖片
*/
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 file
private 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 = "";

@Override
protected 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;
    }
  }
}

/**
* 選擇照片后結(jié)束
*/
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);
}

3、工具類

public class FileUtils {
  /**
   * 把圖片壓縮到200K
   *
   * @param oldpath
   *      壓縮前的圖片路徑
   * @param newPath
   *      壓縮后的圖片路徑
   * @return
   */
  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);
  }

  /**
   * 旋轉(zhuǎn)圖片
   * @param angle
   * @param bitmap
   * @return Bitmap
   */
  public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
    //旋轉(zhuǎn)圖片 動作
    Matrix matrix = new Matrix();;
    matrix.postRotate(angle);
    System.out.println("angle2=" + angle);
    // 創(chuàng)建新的圖片
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
        bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    return resizedBitmap;
  }

  /**
   * 讀取圖片屬性:旋轉(zhuǎn)的角度
   * @param path 圖片絕對路徑
   * @return degree旋轉(zhuǎn)的角度
   */
  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;
  }

  /**
   * 把字節(jié)數(shù)組保存為一個文件
   *
   * @param b
   * @param outputFile
   * @return
   */
  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;
  }

  /**
   * 圖片壓縮
   *
   * @param fPath
   * @return
   */
  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;
  }

 

  /**
   * 創(chuàng)建目錄
   * @param path
   */
  public static void setMkdir(String path)
  {
    File file = new File(path);
    if(!file.exists())
    {
      file.mkdirs();
      Log.e("file", "目錄不存在 創(chuàng)建目錄  ");
    }else{
      Log.e("file", "目錄存在");
    }
  }

  /**
   * 獲取目錄名稱
   * @param url
   * @return FileName
   */
  public static String getFileName(String url)
  {
    int lastIndexStart = url.lastIndexOf("/");
    if(lastIndexStart!=-1)
    {
      return url.substring(lastIndexStart+1, url.length());
    }else{
      return null;
    }
  }

  /**
   * 刪除該目錄下的文件
   *
   * @param path
   */
  public static void delFile(String path) {
    if (!TextUtils.isEmpty(path)) {
      File file = new File(path);
      if (file.exists()) {
        file.delete();
      }
    }
  }
}

4、需要注意的問題

在打release包的時候,因為混淆的問題,點擊又會沒有反應(yīng),這是因為openFileChooser()是系統(tǒng)api,所以需要在混淆是不混淆該方法。

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

當(dāng)點擊拍照之后,如果相機是橫屏拍照的話,當(dāng)拍照結(jié)束之后跳回app的時候,會導(dǎo)致app端當(dāng)前的webView頁面銷毀并重新打開,需要在androidManifest.xml中當(dāng)前Activity添加:

android:configChanges="orientation|keyboardHidden|screenSize"

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于WebView 重定向行為導(dǎo)致的多次加載的問題

    關(guān)于WebView 重定向行為導(dǎo)致的多次加載的問題

    這篇文章主要介紹了關(guān)于WebView 重定向行為導(dǎo)致的多次加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android使用AIDL實現(xiàn)兩個App間通信

    Android使用AIDL實現(xiàn)兩個App間通信

    這篇文章主要為大家詳細介紹了Android使用AIDL實現(xiàn)兩個App間通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android項目實戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實現(xiàn)

    Android項目實戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實現(xiàn)

    這篇文章主要給大家介紹了Android項目實戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實現(xiàn)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • Android MediaPlayer 播放音頻的方式

    Android MediaPlayer 播放音頻的方式

    這篇文章主要介紹了Android MediaPlayer 播放音頻的方式,本文給大家詳細介紹了MediaPlayer的使用方式,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Android入門之源碼開發(fā)基礎(chǔ)教程

    Android入門之源碼開發(fā)基礎(chǔ)教程

    這篇文章主要介紹了Android入門之源碼開發(fā)基礎(chǔ)教程,分析了環(huán)境搭建、模擬器使用及編譯文件的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2016-02-02
  • 談?wù)凙ndroid中的Divider是個什么東東

    談?wù)凙ndroid中的Divider是個什么東東

    在Android應(yīng)用開發(fā)中會經(jīng)常碰到一個叫divider的東西,就是兩個View之間的分割線,本文主要給大家介紹android中的divider相關(guān)知識,需要的朋友可以參考下
    2016-03-03
  • Android雙擊事件攔截方法

    Android雙擊事件攔截方法

    下面小編就為大家分享一篇Android雙擊事件攔截方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android 如何采用Lame編碼器編碼mp3文件

    Android 如何采用Lame編碼器編碼mp3文件

    這篇文章主要介紹了Android 如何采用Lame編碼器編碼mp3文件,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • 淺析SVN在Android Studio中的安裝和配置方法

    淺析SVN在Android Studio中的安裝和配置方法

    這篇文章主要介紹了SVN在Android Studio中的安裝和配置方法,本文通過圖文并茂給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 一文詳解Android無需權(quán)限調(diào)用系統(tǒng)相機拍照

    一文詳解Android無需權(quán)限調(diào)用系統(tǒng)相機拍照

    這篇文章主要為大家介紹了Android無需權(quán)限調(diào)用系統(tǒng)相機拍照詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論