笔记

way

Andriod webview实现文件上传

Andriod 到3.2版本为止,webview方式下使用<input type="file" />点击后都没有反应。实际上顶层是有隐含的接口没实现的,可以自己重写这个方法来实现。以phonegap为例:

public class App extends DroidGap {
    
private ValueCallback<Uri> mUploadMessage;
    
private final static int FILECHOOSER_RESULTCODE = 1;

    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
super.init();
        
// WebView wv = new WebView(this);
        
// wv.setWebViewClient(new WebViewClient());
        this.appView.setWebChromeClient(new CordovaChromeClient(App.this) {
            
// For Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                mUploadMessage 
= uploadMsg;
                Intent i 
= new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType(
"image/*");
                App.
this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            }

            
// The undocumented magic method override
            
// Eclipse will swear at you if you try to put @Override here
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                mUploadMessage 
= uploadMsg;
                Intent i 
= new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType(
"image/*");
                App.
this.startActivityForResult(Intent.createChooser(i, "File Chooser"), App.FILECHOOSER_RESULTCODE);
            }
        });

        
// setContentView(wv);
        super.loadUrl("file:///android_asset/www/login.html");
    }

    @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;
        }
    }
}
如果直接的webview方式,extends WebChromeClient即可。 参考:http://stackoverflow.com/questions/5907369/file-upload-in-webview

posted on 2012-03-12 12:54 yuxh 阅读(5306) 评论(1)  编辑  收藏 所属分类: workAndroid

评论

# re: Andriod webview实现文件上传 2012-08-05 17:32 xiuukin

楼主能不能发个安卓的实例 谢谢啦  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿

随笔分类

随笔档案

收藏夹

博客

搜索

最新评论

阅读排行榜

评论排行榜