找传奇、传世资源到传世资源站!

android_webview_选择手机文件(支持所有_android版本)_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

package com.trend21c.sampleupload;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends ActionBarActivity {

  private ValueCallback<Uri> filePathCallbackNormal;
  private ValueCallback<Uri[]> filePathCallbackLollipop;
  private final static int FILECHOOSER_NORMAL_REQ_CODE = 1;
  private final static int FILECHOOSER_LOLLIPOP_REQ_CODE = 2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView wv = (WebView) findViewById(R.id.webview);
    wv.setWebChromeClient(new WebChromeClient() {
      // For Android < 3.0
      public void openFileChooser( ValueCallback<Uri> uploadMsg) {
        Log.d("MainActivity", "3.0 <");
        openFileChooser(uploadMsg, "");
      }
      // For Android 3.0 
      public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType) {
        Log.d("MainActivity", "3.0 ");
        filePathCallbackNormal = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_NORMAL_REQ_CODE);
      }
      // For Android 4.1 
      public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        Log.d("MainActivity", "4.1 ");
        openFileChooser(uploadMsg, acceptType);
      }

      // For Android 5.0 
      public boolean onShowFileChooser(
          WebView webView, ValueCallback<Uri[]> filePathCallback,
          WebChromeClient.FileChooserParams fileChooserParams) {
        Log.d("MainActivity", "5.0 ");
        if (filePathCallbackLollipop != null) {
          filePathCallbackLollipop.onReceiveValue(null);
          filePathCallbackLollipop = null;
        }
        filePathCallbackLollipop = filePathCallback;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_LOLLIPOP_REQ_CODE);

        return true;
      }
    });
    WebSettings set = wv.getSettings();
    set.setJavaScriptEnabled(true);
    wv.loadUrl("http://test.com/upload.php");
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == FILECHOOSER_NORMAL_REQ_CODE) {
      if (filePathCallbackNormal == null) return ;
      Uri result = (data == null || resultCode != RESULT_OK) ? null : data.getData();
      filePathCallbackNormal.onReceiveValue(result);
      filePathCallbackNormal = null;
    } else if (requestCode == FILECHOOSER_LOLLIPOP_REQ_CODE) {
      if (filePathCallbackLollipop == null) return ;
      filePathCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
      filePathCallbackLollipop = null;
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复