android文件操作与图片压缩

目标:从sdcard中读取图片,并按一定的比例进行缩放,并保存到应用程序的目录下,同时通过ImageView显示保存的图片

分析
android的
文件系统Linux的文件系统是一致的,但是出于一种安全的考虑,应用程序不能随意地创建文件和目录,也就是说应用程序不能随意跨越自己程序的边界,因此,应用程序一般只允许在自身程序的目录下才能进行自由的文件操作。通过Eclipse的DDMS视图可以看到android的应用程序的位置是 /data/data/,而文件则保存在 /data/data//files/目录下。
至于图片压缩,简单是说是按图片按比例进行缩放,android貌似只提供了一个android.graphics.Bitmap类来进行图片处理,所有的
格式(png jpg gif)都可以用Bitmap来表示和存储。

代码:

public void WriteFileEx() {
try {
//获取源图片的大小
Bitmap bm;
BitmapFactory.Options opts = new BitmapFactory.Options();
//当opts不为null时,但decodeFile返回空,不为图片分配
内存,只获取图片的大小,并保存在opts的outWidth和outHeight
BitmapFactory.decodeFile("/sdcard/Stephen.jpg" opts);
int srcWidth = opts.outWidth;
int srcHeight = opts.outHeight;
int destWidth = 0;
int destHeight = 0;
//缩放的比例
double ratio = 0.0;
//tvInfo是一个TextView用于显示图片的尺寸信息
tvInfo.setText("Width:" + srcWidth + " Height:" + srcHeight);
//按比例计算缩放后的图片大小,maxLength是长或宽允许的最大长度
if(srcWidth >srcHeight) {
ratio = srcWidth / maxLength;
destWidth = maxLength;
destHeight = (int) (srcHeight / ratio);
}
else {
ratio = srcHeight / maxLength;
destHeight = maxLength;
destWidth = (int) (srcWidth / ratio); 
}
//对图片进行压缩,是在读取的过程中进行压缩,而不是把图片读进了内存再进行压缩
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//缩放的比例,缩放是很难按准备的比例进行缩放的,目前我只发现只能通过inSampleSize来进行缩放,其值表明缩放的倍数,SDK中建议其值是2的指数值
newOpts.inSampleSize = (int) ratio + 1;
//inJustDecodeBounds设为false表示把图片读进内存中
newOpts.inJustDecodeBounds = false;
//
设置大小,这个一般是不准确的,是以inSampleSize的为准,但是如果不设置却不能缩放
newOpts.outHeight = destHeight;
newOpts.outWidth = destWidth;
//添加尺寸信息,
tvInfo.append("/nWidth:" + newOpts.outWidth + " Height:" + newOpts.outHeight);
//获取缩放后图片
Bitmap destBm = BitmapFactory.decodeFile("/sdcard/Stephen.jpg" newOpts);

if(destBm == null) {
showAlert("CreateFile" 0 "Create Failed" "OK" false);
} else {
//文件命名,通过GUID可避免命名的重复
String fileName = java.util.UUID.randomUUID().toString() + ".jpg";
//另外定义:
//ConfigManager.photoDir = getFileStreamPath(photoDirName) 
//String photoDirName = "photo";要注意是根目录
File destFile = new File(ConfigManager.photoDir fileName);
//创建文件输出流
OutputStream os = new FileOutputStream(destFile);
//存储
destBm.compress(CompressFormat.JPEG 100 os);
//关闭流
os.close();
//显示图片
//setImgView(fileName);
//setDrawable(fileName);
setDrawableAbsolute(fileName);
}
} catch(Exception e) {
showAlert("CreateFile" 0 e.toString() "OK" false);
}
}

 

 

显示图片有多种方式,通过ImageView,可以用Bitmap或者是用Drawable来进行显示。下面的imgView是一个ImageView的引用。

Bitmap通过输入流的方式显示:
public void setImgView(String fileName) {
try
{
File file = new File(ConfigManager.photoDir fileName);
InputStream is = new FileInputStream(file);
Bitmap bm = BitmapFactory.decodeStream(is);
if (bm != null) {
imgView.setImageBitmap(bm);
} else {
showAlert("CreateFile" 0 "Set Failed" "OK" false);
}
is.close();
} catch(Exception e) {
showAlert("CreateFile" 0 e.toString() "OK" false);
}
}

Drawable通过输入流的方式显示:
public void setDrawable(String fileName) {
try {
File file = new File(ConfigManager.photoDir fileName);
InputStream is = new FileInputStream(file);
Drawable da = Drawable.createFromStream(is null);
if (da != null) {
imgView.setImageDrawable(da);
} else {
showAlert("CreateFile" 0 "Set Failed" "OK" false);
}
is.close();
} catch(Exception e) {
showAlert("CreateFile" 0 e.toString() "OK" false);
}
}

直接通过Drawable进行显示:
public void setDrawableAbsolute(String fileName) {
try {
File file = new File(ConfigManager.photoDir fileName);
Drawable da = Drawable.createFromPath(file.getAbsolutePath());
if (da != null) {
imgView.setImageDrawable(da);
} else {
showAlert("CreateFile" 0 "Set Failed" "OK" false);
}
} catch(Exception e) {
showAlert("CreateFile" 0 e.toString() "OK" false);
}
}

 

总的来说,在应用程序的作用域范围内android还是可以比较灵活地进行文件操作,不过可能显示地通过流的方式来操作应该会对程序的性能有一定的影响。在进行具体操作的时候要注意
相对路径和绝对路径的区别,如果获得了应用程序的作用域中的相对路径,可以通过File.getAbsolutePath()的方法来获取完整的绝对路径来进行操作。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值