ImageLoader虽然说是一个相对于比较老的一个框架了 ,但是总的来说,还是比较好用的,今天我就总结了一下它的用法。还有调用系统相册并裁剪,以及,通过sharedpreference和文件存储来保存及拿取图片

这些,我就都结合在了一起  写了个demo.

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ehsure.imageloadertest.MainActivity">
<Button
android:id="@+id/main_photo_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用系统相册"
android:onClick="showImg"
/> <ImageView
android:id="@+id/iv_main_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> MainActivty.class
public class MainActivity extends Activity {
ImageView imageView;
private String updateLogoPath;
private String userName;
private String userPhone;
private String userToken;
private long userId;
private String imgPath;
private File image;
private String newName; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.iv_main_img);
/*
(这一段都是说明ImageLoader的用法)
*/
try{
String newUrl = "http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg";
ImageLoader imageloader=ImageLoader.getInstance();
/*
1.这个是最简单的显示方法
*/
//使用默认的ImageLoaderConfiguration
ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(this.getApplicationContext());
//初始化ImageLoader的配置
imageloader.init(configuration);
//加载图片
imageloader.displayImage(newUrl, imageView);
/*
2.这个是使用自定义的配置来达到自己想要显示的效果
*/
//使用自定义的configuration
// ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext())
// .threadPriority(Thread.NORM_PRIORITY - 2)//设置线程优先级
// .threadPoolSize(4)//线程池内加载的数量,推荐范围1-5内。
// .denyCacheImageMultipleSizesInMemory()//当同一个Uri获取不同大小的图片缓存到内存中时只缓存一个。不设置的话默认会缓存多个不同大小的图片
// .memoryCacheExtraOptions(480, 800)//内存缓存文件的最大长度
// .memoryCache(new LruMemoryCache(10 * 1024 * 1024))//内存缓存方式,这里可以换成自己的内存缓存实现。(推荐LruMemoryCache,道理自己懂的)
// .memoryCacheSize(10 * 1024 * 1024)
// .discCache(new UnlimitedDiscCache(createSavePath())) //内存缓存的最大值
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
// .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000))//设置连接时间5s,超时时间30s
// .writeDebugLogs()
// .build();
//// 初始化ImageLoader的配置
// imageloader.init(configuration);
// //加载图片
// imageloader.displayImage(newUrl, imageView,setImageOptionsConfig());
}catch (Exception e){
e.printStackTrace();
}
} /**
* 配置图片加载时候的配置,在实际开发中可以对这些参数进行一次封装。
*/
public DisplayImageOptions setImageOptionsConfig(){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)//设置图片在下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//设置图片Uri为null或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher)//设置图片加载/解码过程中错误时显示的图片
.cacheInMemory(true)//设置下载的图片是否缓存在内存中
.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
.considerExifParams(true)//是否考虑JPEG图像的旋转,翻转
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.ARGB_8888)//设置图片的解码类型
.resetViewBeforeLoading(true)//设置图片在下载前是否重置和复位
.displayer(new SimpleBitmapDisplayer())//不设置的时候是默认的
//.displayer(new RoundedBitmapDisplayer(20))//是否为圆角,弧度是多少
//displayer()还可以设置渐入动画
.build();
return options;
} /*
下面这这一段是指如何调用系统相册,并裁剪显示
*/ public void showImg(View view){
switch (view.getId()) {
case R.id.main_photo_bt:
//这里是跳转到系统相册页面
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
break;
}
} /*
* 开始图片裁剪
*/
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK && data != null) {
//这里是从图库拿到图片之后调用系统的裁剪方法
startPhotoZoom(data.getData(),150);
}
}else if(requestCode==1){
if(data!=null){
Bundle bundle = data.getExtras();
if(bundle!=null){
//这是最终从图库拿到的并且已经裁剪的图片
Bitmap bitmap = data.getParcelableExtra("data");
imageView.setImageBitmap(bitmap);
}
}
}
} /*
这里是通过sharedpreference和文件存储来保存及拿取图片的
*/ /*
* 创建图片缓存在手机的文件
*/
public File createFile(Bitmap bitmap){
String path = "";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
path = Environment.getExternalStorageDirectory().getPath() + "/imageLoaderTest";
} else {
path = "/imageLoaderTest";
}
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
path = path+"/"+getPhotoFileName();
file = new File(path);
Bitmap.CompressFormat cf = Bitmap.CompressFormat.JPEG;
try {
OutputStream os = new FileOutputStream(file);
boolean flag =bitmap.compress(cf, 100, os);
if(flag==true){
return file;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null; } // 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
} /* 将bitmap转换成字节
*/
public String bitmapToString(Bitmap bitmap) {
String str = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 1, outputStream);
byte[] bytes = outputStream.toByteArray();
str = Base64.encodeToString(bytes, Base64.DEFAULT);
return str;
} /*
* 将字符串转换为bitmap
*/
public Bitmap stringToBitmap(String str) {
byte[] bytes = Base64.decode(str, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
}
 
其中
getPhotoFileName 和 stringToBitmap这两个方法实现的是bitmap和String间的互转,开发者拿到bitmap可将其转换成String存入SharedPreference,拿取的时候也可通过拿到的String将其转成bitmap.

这种方式通常在上传头像中用得比较常见


ImageLoader框架的使用、调用系统相册显示图片并裁剪显示、保存图片的两种方式的更多相关文章

  1. IOS 调用系统相册或照相机tab按钮显示中文

  2. iOS调用系统相册、相机 显示中文标题

    解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed          YES 表 ...

  3. iOS 调用系统相册 相机 时,显示中文标题

    解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed YES 表示是否允许应用程序 ...

  4. ios调用系统相册、相机 显示中文标题、本地化多语言支持

    因为调用系统相册.相机需要显示中文,所以搞了半天才知道是在Project->info->Custom ios Target Properties 添加 Localizations 并加入C ...

  5. Android 调用系统相机拍照保存以及调用系统相册的方法

    系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的.下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法. 首先看看调用系统相机的核心方法: Intent camera = new ...

  6. Swift—调用系统相册和相机

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  7. Android调用系统相册和拍照的Demo

    最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...

  8. APP调用系统相册,使用3DTouch重压,崩溃

    崩溃:app调用系统相册,使用3DTouch重压,崩溃 问题描述 app调用系统相册,使用3DTouch重压,一般的app都会崩溃. 解决方法 写个分类即可 @implementation UICol ...

  9. 在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题

    在调用系统相册时,UIIMagePickerController使用中偷换StatusBar颜色的问题 此时解决办法是 #pragma mark - UIImagePickerController D ...

随机推荐

  1. CentOS7.1下JDK+Tomcat应用环境搭建

    最近由于项目的原因,需要测试Linux环境下的应用部署情况.选用了CentOS7.1版本+JDK1.6版本+Tomcat7.0版本进行搭建.其间各种折磨就不说了,随手把相关的注意事项和大概的步骤记录下 ...

  2. 端口限制情况下php+xdebug环境配置

    PHP程序在开发的时候调试是比较方便的,大体情况下,输出,打log是可以解决几乎所有问题. 但是还不够,有些问题,用打log的形式定位问题是相当痛苦的事情,有些时候测试环境没配好的话,你可能需要做许多 ...

  3. HDU2459 后缀数组+RMQ

    题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...

  4. NGUI屏幕自适应

    NGUI确实是非常棒的一个做界面的插件,比起U3D自带的GUI要好很多,当然也有一些不好之处,毕竟什么都不可能那么完美. 最近在用Unity写游戏使用NGUI遇到了一个很多人都在遇到的问题,就是关于屏 ...

  5. export a java project to runable jar

    When a java project needs to be transfered to another machine, e.g. vps, we need to export it to a r ...

  6. 在WPF中获取DataGridTemplateColumn模板定义的内容控件

    xaml格式描述: <DataGrid Name="dataGrid" Grid.Row="1" ItemsSource="{Binding}& ...

  7. HYSBZ 2243

    //Accepted 18440 KB 5556 ms /* source:HYSBZ 2243 time :2015.5.29 by :songt */ /*题解: 树链剖分 */ #include ...

  8. 微信平台上遇到的bug

    做微信平台遇到的bug,没有什么方法修改,至今只是避免出现,还未解决 1.header的position:fixed定位:如果整个页面的高度不足屏幕高度时,安卓部分手机header与title之间会有 ...

  9. web兼容行探究1:IE 6 select节点显示在绝对布局之上的解决方法

    解决方式就是在绝对布局的元素下放置一个一样大小的iframe元素,这样iframe可以将select盖住,同时解决了问题. 源码如下: <!-- IE6BUG select在絕對定位的元素之上顯 ...

  10. 解决Oracle+weblogic系统死机的问题

    前段时间发布的系统(Oracle+weblogic)频繁挂掉,每天早上9点.下午2点高峰期就挂,纠结了很长时间,最终解决,方法描述下. 执行select count(*),status from v$ ...