android文件上传源码,android webview 网页文件上传 实例源码(android studio版)

【实例简介】

【实例截图】

【核心代码】

package com.example.bajian.webviewuploaddemo;

import android.app.Activity;

import android.content.Intent;

import android.content.res.Configuration;

import android.graphics.Bitmap;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.webkit.ValueCallback;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.ProgressBar;

/**

* 网上找的webview设置,自己写的链接测试下网页文件上传

*/

public class MainActivity extends Activity {

WebView web;

ProgressBar progressBar;

private ValueCallback mUploadMessage;

private final static int FILECHOOSER_RESULTCODE=1;

@Override

protected void onActivityResult(int requestCode, int resultCode,

Intent intent) {

if(requestCode==FILECHOOSER_RESULTCODE)

{

if (null == mUploadMessage) return;

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

: intent.getData();

mUploadMessage.onReceiveValue(result);

mUploadMessage = null;

}

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

web = (WebView) findViewById(R.id.webview01);

progressBar = (ProgressBar) findViewById(R.id.progressBar1);

web = new WebView(this);

web.getSettings().setJavaScriptEnabled(true);

web.loadUrl("http://bxj.snewfly.com/web_upload_demo/upload.html");

web.setWebViewClient(new myWebClient());

web.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 uploadMsg) {

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

MainActivity.this.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("*/*");

MainActivity.this.startActivityForResult(

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

FILECHOOSER_RESULTCODE);

}

//For Android 4.1

public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture){

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("*/*");

// i.setType("image/*");

MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );

}

});

setContentView(web);

}

public class myWebClient extends WebViewClient

{

@Override

public void onPageStarted(WebView view, String url, Bitmap favicon) {

// TODO Auto-generated method stub

super.onPageStarted(view, url, favicon);

}

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

// TODO Auto-generated method stub

view.loadUrl(url);

return true;

}

@Override

public void onPageFinished(WebView view, String url) {

// TODO Auto-generated method stub

super.onPageFinished(view, url);

progressBar.setVisibility(View.GONE);

}

}

//flipscreen not loading again

@Override

public void onConfigurationChanged(Configuration newConfig){

super.onConfigurationChanged(newConfig);

}

// To handle "Back" key press event for WebView to go back to previous screen.

/*@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

{

if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {

web.goBack();

return true;

}

return super.onKeyDown(keyCode, event);

}*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值