Android WebView Method to Call Local Album

  • 2021-10-25 07:48:25
  • OfStack

This article example for everyone to share Android WebView call local photo album specific implementation method, for your reference, the specific content is as follows

First of all, we must know that WebView of android itself does not support calling mobile phone files and uploading them. Secondly, the kernel of WebView is not very similar when android is updated every time, which is also enough; However, the requirements can not be changed, H5 needs to call the system album, but finally find a way to solve, is to rewrite inside a method, but this situation is also effective before 5.0, 5.0 after the need to rewrite another method, first of all, these methods are listed
Note that the method we need to override here is in this WebChromeClient class;


private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mUploadCallbackAboveL;
private final static int FILECHOOSER_RESULTCODE = 101;

When we are under 5.0, we use the following three methods:


public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  Log.d(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg)");
  mUploadMessage = uploadMsg;
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  i.addCategory(Intent.CATEGORY_OPENABLE);
  i.setType("image/*");
  Html5Activity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"),
      FILECHOOSER_RESULTCODE);
}

public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
  Log.d(TAG, "openFileChoose( ValueCallback uploadMsg, String acceptType )");
  mUploadMessage = uploadMsg;
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  i.addCategory(Intent.CATEGORY_OPENABLE);
  i.setType("image/*");
  Html5Activity.this.startActivityForResult(
      Intent.createChooser(i, "File Browser"),
      FILECHOOSER_RESULTCODE);
}

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
  Log.d(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg, String acceptType, String capture)");
  mUploadMessage = uploadMsg;
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  i.addCategory(Intent.CATEGORY_OPENABLE);
  i.setType("image/*");
  Html5Activity.this.startActivityForResult(Intent.createChooser(i, "File Browser"),
      FILECHOOSER_RESULTCODE);
}

It is worth noting that these three methods are all the same, but they will be called separately under different versions, and this method is to rewrite this WebChromeClient class. Don't think that we wrote it casually, but google doesn't want us to rewrite this method, but it is not the same after 5.0. What needs to be rewritten is the following method:


@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
  mUploadCallbackAboveL = filePathCallback;
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  i.addCategory(Intent.CATEGORY_OPENABLE);
  i.setType("image/*");
  Html5Activity.this.startActivityForResult(
      Intent.createChooser(i, "File Browser"),
      FILECHOOSER_RESULTCODE);
  return true;
}

Ok, at this step, our call has been ok, but there is still a required process; Like our common callback reception, it is 1 mode 1:


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

Of course, there is another one for us to use in 5.0:


private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {
  if (requestCode != FILECHOOSER_RESULTCODE
      || mUploadCallbackAboveL == null) {
    return;
  }
  Uri[] results = null;
  if (resultCode == Activity.RESULT_OK) {
    if (data == null) {
    } else {
      String dataString = data.getDataString();
      ClipData clipData = data.getClipData();
      if (clipData != null) {
        results = new Uri[clipData.getItemCount()];
        for (int i = 0; i < clipData.getItemCount(); i++) {
          ClipData.Item item = clipData.getItemAt(i);
          results[i] = item.getUri();
          Log.e(TAG, "onActivityResultAboveL: " + results[i].getPath());
        }
      }
      if (dataString != null)
        results = new Uri[]{Uri.parse(dataString)};
      Log.e(TAG, "onActivityResultAboveL: " + results.length);
    }
  }
  mUploadCallbackAboveL.onReceiveValue(results);
  mUploadCallbackAboveL = null;
  return;
}

In this step, H5 can basically call the photo album of the mobile phone normally.


Related articles: