直接上代码,体会Webview强大的功能。

webView.setWebChromeClient(webChromeClient);private String mCameraFilePath = null;

private ValueCallback mUploadMessage;// 表单的数据信息

private final static int FILECHOOSER_RESULTCODE = 1;// 表单的结果回调private WebChromeClient webChromeClient = new WebChromeClient() {

// For Android 3.0+

public void openFileChooser(ValueCallback uploadMsg) {

if (uploadMsg == null) {

return;

}

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

activity.startActivityForResult(

Intent.createChooser(i, "File Chooser"),

FILECHOOSER_RESULTCODE);

}

// For Android 3.0+

public void openFileChooser(ValueCallback uploadMsg, String acceptType) {

if (uploadMsg == null) {

return;

}

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("*/*");

activity.startActivityForResult(

Intent.createChooser(i, "File Browser"),

FILECHOOSER_RESULTCODE);

}

// For Android 4.1

public void openFileChooser(ValueCallback uploadMsg,

String acceptType, String capture) {

if (uploadMsg == null) {

return;

}

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

activity.startActivityForResult(

Intent.createChooser(i, "File Chooser"),

FILECHOOSER_RESULTCODE);

}

private Intent createDefaultOpenableIntent() {

// Create and return a chooser with the default OPENABLE

// actions including the camera, camcorder and sound

// recorder where available.

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("*/*");

Intent chooser = createChooserIntent(createCameraIntent(),

createCamcorderIntent(), createSoundRecorderIntent());

chooser.putExtra(Intent.EXTRA_INTENT, i);

return chooser;

}

private Intent createChooserIntent(Intent... intents) {

Intent chooser = new Intent(Intent.ACTION_CHOOSER);

chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);

chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser");

return chooser;

}

private Intent createCameraIntent() {

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File externalDataDir = Environment

.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

System.out.println("externalDataDir:" + externalDataDir);

File cameraDataDir = new File(externalDataDir.getAbsolutePath()

+ File.separator + "browser-photo");

cameraDataDir.mkdirs();

mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator

+ System.currentTimeMillis() + ".jpg";

System.out.println("mcamerafilepath:" + mCameraFilePath);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,

Uri.fromFile(new File(mCameraFilePath)));

return cameraIntent;

}

private Intent createCamcorderIntent() {

return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

}

private Intent createSoundRecorderIntent() {

return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);

}

};

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

if (requestCode == FILECHOOSER_RESULTCODE) {

if (null == mUploadMessage)

return;

Uri result = intent == null || resultCode != Activity.RESULT_OK ? null

: intent.getData();

if (result == null && intent == null

&& resultCode == Activity.RESULT_OK) {

File cameraFile = new File(mCameraFilePath);

if (cameraFile.exists()) {

result = Uri.fromFile(cameraFile);

activity.sendBroadcast(new Intent(

Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));

}

}

mUploadMessage.onReceiveValue(result);

mUploadMessage = null;

}

}

原文:http://blog.csdn.net/xiechengfa/article/details/45376363

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐