android实例 文件选择器,android webview 选择手机文件(支持所有 android版本)

【实例简介】

【实例截图】

【核心代码】

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 filePathCallbackNormal;

private ValueCallback 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 uploadMsg) {

Log.d("MainActivity", "3.0

openFileChooser(uploadMsg, "");

}

// For Android 3.0

public void openFileChooser( ValueCallback 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 uploadMsg, String acceptType, String capture) {

Log.d("MainActivity", "4.1 ");

openFileChooser(uploadMsg, acceptType);

}

// For Android 5.0

public boolean onShowFileChooser(

WebView webView, ValueCallback 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值