BitmapUtils
- 能设置图片大小,避免oom(out of memory--内存溢出)
- 基于lru(最近最少使用算法)的三级缓存(网络,内存,本地),访问速度较快
- 避免图片错位(在listview或者recyclerview中使用)
- 图片列表快速滑动时,可以取消加载滑出屏幕的图片
1.需要添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.创建BitmapUtils
/**
* 使用默认配置创建
*/
protected void createBitmapUtils()
{
mBmUtils = new BitmapUtils(this);
}
/**
* 自定义各种配置
*/
protected void createBitmapUtilsWithCfg(){
//指定本地缓存路径以及内存缓存大小
int iSize = (int) (Runtime.getRuntime().maxMemory()/8);
mBmUtils = new BitmapUtils(this,"/sdcard/bmcc",iSize);
//线程池中线程数量
mBmUtils.configThreadPoolSize(4);
//加载失败图片
mBmUtils.configDefaultLoadFailedImage(R.mipmap.ic_launcher);
//加载中图片
mBmUtils.configDefaultLoadingImage(R.mipmap.ic_launcher);
//连接超时时间
mBmUtils.configDefaultConnectTimeout(30000);
//设置图片大小
mBmUtils.configDefaultBitmapMaxSize(300,300);
}
3.加载图片
protected void InitImageView() {
//加载网络图片,默认本地缓存路径:/storage/emulated/0/Android/data/包名/cache/xBitmapCache
mBmUtils.display(mIvTest,
"http://pic12.nipic.com/20110217/6757620_105953632124_2.jpg");
//加载本地图片
//bmUtils.display(mIvTest,"/sdcard/aaa.jpg");
//加载assets下面的图片
//bmUtils.display(mIvTest,"assets/abc.jpg");
}
4.listview滑动时不加载图片(需要设置2个默认图片,否则可能挤到一起)
protected void InitListView(){
List<String> listUrls = new ArrayList<String>();
//为了看到演示效果,清除并关闭缓存
mBmUtils.clearCache();
mBmUtils.configDiskCacheEnabled(false);
mBmUtils.configMemoryCacheEnabled(false);
for (int i = 0; i < 20; i++) {
listUrls.add("http://pic12.nipic.com/20110217/6757620_105953632124_2.jpg");
}
MyAdapter<String> adapter = new MyAdapter<String>(this,listUrls,mBmUtils);
mLvTest.setAdapter(adapter);
//滑动(手指没有离开屏幕)和快速滑动(手指离开屏幕)都不加载图片,滑动时可以看到效果
mLvTest.setOnScrollListener(new PauseOnScrollListener(mBmUtils, true, true));
}
5.Bitmap的自定义显示:
BitmapUtilsActivity.bmUtils.display(iv, url, new BitmapLoadCallBack<ImageView>() {
@Override
public void onLoadCompleted(ImageView container, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
Matrix matrix = new Matrix();
matrix.reset();
matrix.postRotate(90);
Bitmap bmRotate = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight()
,matrix,true);
container.setImageBitmap(bmRotate);
}
@Override
public void onLoadFailed(ImageView container, String uri, Drawable drawable) {
}
});
httputils
- 需要访问网络权限和写存储卡权限
- httputils使用httpclient,最新版本的sdk中没有官方集成httpclient
- 可以降低编译版本(比如22)
- 也可以导入httpclient的jar包,可以从http://hc.apache.org/downloads.cgi下载(httpcore和httpclient),也可以在as中搜索httpclient,选择com.hynnet:httpclient:4.5.1,下载并导入后,会出现冲突,需要在模块的gradle中的android块下配置包编译属性,排除冲突的配置文件:
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
- 创建httputils
mHttpUtils = new HttpUtils();//最好整个应用一个实例
- 配置
/**
* 给httputils配置各种参数
*/
void configUtils(){
//设置线程池数量
mHttpUtils.configRequestThreadPoolSize(4);
//设置请求重试次数
mHttpUtils.configRequestRetryCount(3);
//设置请求超时时间
mHttpUtils.configTimeout(30000);
}
- 调用get方法获取数据
/**
* 使用get方法获取数据
*/
void httpGet(){
mHttpUtils.send(HttpRequest.HttpMethod.GET, "http://op.juhe.cn/yi18/news/newsclass?key=c90b25c724fea7c5d6207e352fb03c63", new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Log.d("qfhttpget", responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
Log.d("qfhttpget", msg);
}
});
}
- get方法也可以设置param
/**
*通过设置param来获取get数据
*/
void httpGetBySetParam(){
RequestParams requestParams = new RequestParams();
NameValuePair nameValuePair = new BasicNameValuePair("key","c90b25c724fea7c5d6207e352fb03c63");
requestParams.addQueryStringParameter(nameValuePair);
mHttpUtils.send(HttpRequest.HttpMethod.GET, "http://op.juhe.cn/yi18/news/newsclass", requestParams, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Log.d("qfhttpGetBySetParam",responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
Log.d("qfhttpGetBySetParam",msg);
}
});
}
- 调用post方法获取数据
/**
* post请求
*/
void httpPost(){
String strUrl = "http://op.juhe.cn/yi18/news/newsclass";
RequestParams requestParams = new RequestParams();
requestParams.addBodyParameter("key", "c90b25c724fea7c5d6207e352fb03c63");
//也可以用NameValuePair
// NameValuePair paramsPair = new BasicNameValuePair("key","c90b25c724fea7c5d6207e352fb03c63");
// requestParams.addBodyParameter(paramsPair);
mHttpUtils.send(HttpRequest.HttpMethod.POST, strUrl, requestParams, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Log.d("qfhttpPost",responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
}
});
}
- 文件下载
/**
* 文件下载
*/
void httpDowload(){
String strUrl = "http://apache.fayea.com//httpcomponents/httpclient/binary/httpcomponents-client-4.5.1-bin.zip";
mHttpUtils.download(strUrl, "/sdcard/httpclient.zip", new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
}
@Override
public void onFailure(HttpException error, String msg) {
Log.d("qfhttpDowload", msg);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
Log.d("qfhttpDowload", ((current * 100) / total) + "%\n");
}
});
}
- 文件上传
- 1.mac下apache服务器的操作:
启动:sudo apachectl start
停止:sudo apachectl stop
重启:sudo apachectl restart
web根目录:/Library/WebServer/Documents/
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin
2.upload.php实现上传,支持多文件上传
3.实现上传
/**
* 文件上传
*/
void httpUpload(){
String strHost = "http://10.2.169.118/upload.php";
RequestParams requestParams = new RequestParams();
requestParams.addBodyParameter("upload_file1",new File("/sdcard/aaa.jpg"));
requestParams.addBodyParameter("upload_file2",new File("/sdcard/mydb"));
mHttpUtils.send(HttpRequest.HttpMethod.POST, strHost, requestParams, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Log.d("qfhttphttpUpload", "ok");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
Log.d("qfhttphttpUpload", ((current * 100) / total) + "%\n");
}
@Override
public void onFailure(HttpException error, String msg) {
Log.d("qfhttphttpUpload", msg);
}
});
}