2016年3月30日星期三

剪切图片 CropImageView

https://github.com/cesards/CropImageView

2016年3月28日星期一

系统分享代码

String type = "image/*";String filename = "/dog.jpg";String mediaPath = Environment.getExternalStorageDirectory() + filename;


createInstagramIntent(type, mediaPath);

private void createInstagramIntent(String type, String mediaPath){

    // Create the new Intent using the 'Send' action.    Intent share = new Intent(Intent.ACTION_SEND);
    // Set the MIME type    share.setType(type);
    // Create the URI from the media    File media = new File(mediaPath);    Uri uri = Uri.fromFile(media);
    // Add the URI to the Intent.    share.putExtra(Intent.EXTRA_STREAM, uri);
    // Broadcast the Intent.    startActivity(Intent.createChooser(share, "Share to"));}

使用此方法弹出的是 系统中可以分享的代码

2016年3月26日星期六

MAC KEYMAP 常用快捷键

Settings->KeyMap->Main Menu->Completion-Basic : 关键字自动补齐查询
Settings->KeyMap->Main Menu->Code->Generate ... : 设置GET/SET等方法窗口
MainMenu->Find->Replace in Path:全局搜索

2016年3月17日星期四

Webview 设置 cookie

因项目需要,需要在App中嵌入网页,使用Nativie方式登录,然后将cookie保存到WebView中,实现免登录功能。同步Cookie到WebView的方法网上有大量的参考资料,也可以参考下面的代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Sync Cookie
*/
private void syncCookie(Context context, String url){
        try{
            Log.d("Nat: webView.syncCookie.url", url);           
 
            CookieSyncManager.createInstance(context);
 
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            cookieManager.removeSessionCookie();// 移除
            cookieManager.removeAllCookie();
            String oldCookie = cookieManager.getCookie(url);
            if(oldCookie != null){
                Log.d("Nat: webView.syncCookieOutter.oldCookie", oldCookie);
            }
 
            StringBuilder sbCookie = new StringBuilder();
            sbCookie.append(String.format("JSESSIONID=%s","INPUT YOUR JSESSIONID STRING"));
            sbCookie.append(String.format(";domain=%s""INPUT YOUR DOMAIN STRING"));
            sbCookie.append(String.format(";path=%s","INPUT YOUR PATH STRING"));
 
            String cookieValue = sbCookie.toString();
            cookieManager.setCookie(url, cookieValue);
            CookieSyncManager.getInstance().sync(); 
 
            String newCookie = cookieManager.getCookie(url);
            if(newCookie != null){
                Log.d("Nat: webView.syncCookie.newCookie", newCookie);
            }
        }catch(Exception e){
            Log.e("Nat: webView.syncCookie failed", e.toString());
        }
    }

使用上面的方法可以将Cookie同步到WebView中,这样浏览网页时即可实现免登录。
但是在实际使用过程中发现Cookie并未保存成功,每次都会跳转到登录页面,纠结了很久,终于发现是在初始化WebView时漏掉了重要的东西。可以参考下面我的代码设置WebView。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* init WebView Settings
* */
    private void initWebViewSettings(){
//        myWebView.getSettings().setSupportZoom(true);
//        myWebView.getSettings().setBuiltInZoomControls(true);
//        myWebView.getSettings().setDefaultFontSize(12);
//        myWebView.getSettings().setLoadWithOverviewMode(true);
        // 设置可以访问文件
        myWebView.getSettings().setAllowFileAccess(true);
        //如果访问的页面中有Javascript,则webview必须设置支持Javascript
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setUserAgentString(MyApplication.getUserAgent());
        myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        myWebView.getSettings().setAllowFileAccess(true);
        myWebView.getSettings().setAppCacheEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setDatabaseEnabled(true);
    }

完成以上两步操作,再次运行程序,你会发现,打开网页后不会再跳转到登录页面了。
第一使用WebView控件,原以为很简单,可是一不小心就掉坑里去了,大家小心。