基本信息
源码名称:android webview 选择手机文件(支持所有 android版本)
源码大小:0.09M
文件格式:.zip
开发语言:Java
更新时间:2016-03-03
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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);
}
}