我使用了两个片段的tabhost。一个片段用于列表,另一个用于webview。从那个片段我必须从库上传文件。以下是我的webview片段。
public class AppleFragment extends Fragment{
private static final String TAG = "SocialActivityFragment";
//protected static final int FILECHOOSER_RESULTCODE = 0;
WebView browser;
static String cookieString;
ProgressBar Pbar;
HashMap<String, String> headerMap = new HashMap<String, String>();
private ValueCallback<Uri> mUploadMessage;
final static int FILECHOOSER_RESULTCODE=1;
@Override
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();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), R.layout.social_activities_layout, apple_versions);
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.social_layout, null);
Pbar = (ProgressBar) layout.findViewById(R.id.pB1);
String strResponse = util.makeWebCall("http://google.com");
DefaultHttpClient mClient = util.getClient();
Cookie sessionInfo;
List<Cookie> cookies = mClient.getCookieStore().getCookies();
if (! cookies.isEmpty()){
CookieSyncManager.createInstance(getActivity().getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
for(Cookie cookie : cookies){
sessionInfo = cookie;
cookieString = sessionInfo.getName() + "=" + Sessions.getPhpSessid(getActivity().getApplicationContext()) + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie("http://youornot.com/social/", cookieString);
CookieSyncManager.getInstance().sync();
//Toast.makeText(getApplicationContext(), cookieString, Toast.LENGTH_LONG).show();
}
}
AlertDialogManager alert=new AlertDialogManager();
ConnectionDetector cd=new ConnectionDetector(getActivity().getApplicationContext());
if(!cd.isConnectedToInternet())
{
alert.showAlertDialog(getActivity(), "Error..!", "You have not Connected to Internet", true);
}
browser=(WebView)layout.findViewById(R.id.webBrowser);
// WebView browser = new WebView(SocialActivity.this);
WebSettings settings = browser.getSettings();
browser.setScrollBarStyle(browser.OVER_SCROLL_IF_CONTENT_SCROLLS);
WebViewClient wvClient = new WebViewClient();
browser.setWebViewClient(wvClient);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setWebChromeClient(new WebChromeClient() {
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
getActivity().startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
@SuppressWarnings("unused")
public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
getActivity().startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ),FILECHOOSER_RESULTCODE );
}
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
//txtview.setVisibility(View.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
// txtview.setVisibility(View.GONE);
}
}
});
headerMap.put("Cookie", cookieString);
browser.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
//The key is this line.
v.requestFocusFromTouch();
break;
}
return false;
}
});
browser.loadUrl("http://google.com", headerMap);
return layout;//super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.web_menu, menu);
}
@SuppressLint("NewApi")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.refresh:
browser.reload();
// browser.loadUrl("http://google.com", headerMap);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
每当我上传文件时,webview根本没有响应。当我关闭应用程序时,webview也没有响应。那么我如何在tabview片段中的webview上传文件。
提前致谢。
答案 0 :(得分:1)
公共类AppleFragment扩展了Fragment {
WebView webView;
//make HTML upload button work in Webview
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != getActivity().RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_web, container, false);
// Web View
webView = (WebView) view.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient(){
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
getActivity().startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
getActivity().startActivityForResult(Intent.createChooser(i, "File Browser"),FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(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/*");
getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
}
});
webView.loadUrl(yourUrl);
return view;
}
}
答案 1 :(得分:0)
公共类FragmentWeb扩展了Fragment {
@覆盖
public void onActivityResult(int requestCode,int resultCode,Intent intent){
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == this.mUploadMessage) {
return;
}
Uri result=null;
try{
if (resultCode != getActivity().RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = intent == null ? mCapturedImageURI : intent.getData();
}
}
catch(Exception e)
{
Toast.makeText(getActivity().getApplicationContext(), "activity :"+e, Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
。 。 。 。 。 }
并且您还需要在MainActivity中提出以下代码
公共类MainActivity扩展了Activity {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
。 。 。 。 }