参考文档:
http://jiangzhengjun.iteye.com/blog/506041 Java中十六进制转换 Integer.toHexString()
http://blog.csdn.net/ma1kong/article/details/2662997 MessageDigest的功能及用法 .
http://www.linuxidc.com/Linux/2011-09/42288.htm Android中使用SoftReference缓存图片对象
http://apps.hi.baidu.com/share/detail/46467495 HttpClient 4处理文件上传的例子(MultipartEntity)
http://support.microsoft.com/kb/257591 安全套接字层 (SSL) 握手说明
必备知识:
&和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false。
&&还具有短路的功能,即如果第一个表达式为false,则不再计算第二个表达式,例如,对于if(str != null && !str.equals(“”))表达式,当str为null时,后面的表达式不会执行,所以不会出现NullPointerException如果将&&改为&,则会抛出NullPointerException异常。If(x==33 & ++y>0) y会增长,If(x==33 && ++y>0)不会增长
&还可以用作位运算符,当&操作符两边的表达式不是boolean类型时,&表示按位与操作,我们通常使用0x0f来与一个整数进行&运算,来获取该整数的最低4个bit位,例如,0x31 & 0x0f的结果为0x01。
代码部分:
取得图片流程:url加密..进入图片缓存寻找..未果..通过httpRequest..附带MultipartEntity,时间太晚,不做分类加注释了,自己看吧
/** * 获得指定file/URL对应的Bitmap,首先找本地文件,如果有直接使用,否则去网上获取 * * @param file * file URL/file PATH * @param bitmap * @param quality * @throws HttpException */ public Bitmap safeGet(String file) throws HttpException { String hashedUrl = getMd5(url); FileInputStream fis = null; fis = mContext.openFileInput(hashedUrl); bitmap=BitmapFactory.decodeStream(fis); if (bitmap != null) { synchronized (this) { // memory cache //private Map<String, SoftReference<Bitmap>> mCache; mCache.put(file, new SoftReference<Bitmap>(bitmap)); } return bitmap; } else { // get from web String url = file; bitmap = downloadImage2(url); // 注释掉以测试新的写入文件方法 // put(file, bitmap); // file Cache return bitmap; } } // MD5 hases are used to generate filenames based off a URL. private String getMd5(String url) { mDigest.update(url.getBytes()); return getHashString(mDigest); } private String getHashString(MessageDigest digest) { StringBuilder builder = new StringBuilder(); for (byte b : digest.digest()) { builder.append(Integer.toHexString((b >> 4) & 0xf)); builder.append(Integer.toHexString(b & 0xf)); } return builder.toString(); } public Response httpRequest(String url, ArrayList<BasicNameValuePair> postParams, File file, boolean authenticated, String httpMethod) throws HttpException { Log.d(TAG, "Sending " + httpMethod + " request to " + url); if (TwitterApplication.DEBUG) { DebugTimer.betweenStart("HTTP"); } URI uri = createURI(url); HttpResponse response = null; Response res = null; HttpUriRequest method = null; // Create POST, GET or DELETE METHOD method = createMethod(httpMethod, uri, file, postParams); // Setup ConnectionParams, Request Headers SetupHTTPConnectionParams(method); // Execute Request try { // 加入OAuth认证信息 if (authenticated) { mOAuthClient.signRequest(method); } response = mClient.execute(method, localcontext); res = new Response(response); } catch (ClientProtocolException e) { Log.e(TAG, e.getMessage(), e); throw new HttpException(e.getMessage(), e); } catch (IOException ioe) { throw new HttpException(ioe.getMessage(), ioe); } catch (OAuthClientException e) { Log.e(TAG, e.getMessage(), e); throw new HttpException(e.getMessage(), e); } if (response != null) { int statusCode = response.getStatusLine().getStatusCode(); // It will throw a weiboException while status code is not 200 HandleResponseStatusCode(statusCode, res); } else { Log.e(TAG, "response is null"); } if (TwitterApplication.DEBUG) { DebugTimer.betweenEnd("HTTP"); } return res; } /** * 创建可带一个File的MultipartEntity * * @param filename * 文件名 * @param file * 文件 * @param postParams * 其他POST参数 * @return 带文件和其他参数的Entity * @throws UnsupportedEncodingException */ private MultipartEntity createMultipartEntity(String filename, File file, ArrayList<BasicNameValuePair> postParams) throws UnsupportedEncodingException { MultipartEntity entity = new MultipartEntity(); // Don't try this. Server does not appear to support chunking. // entity.addPart("media", new InputStreamBody(imageStream, "media")); entity.addPart(filename, new FileBody(file)); for (BasicNameValuePair param : postParams) { entity.addPart(param.getName(), new StringBody(param.getValue())); } return entity; }