android文件操作與圖片壓縮
目標:從sdcard中讀取圖片,並按一定的比例進行縮放,並保存到應用程序的目錄下,同時通過ImageView顯示保存的圖片
分析:
android的文件係統與Linux的文件係統是一致的,但是出於一種安全的考慮,應用程序不能隨意地創建文件和目錄,也就是說應用程序不能隨意跨越自己程序的邊界,因此,應用程序一般隻允許在自身程序的目錄下才能進行自由的文件操作。通過Eclipse的DDMS視圖可以看到android的應用程序的位置是
/data/data/,而文件則保存在 /data/data//files/目錄下。
至於圖片壓縮,簡單是說是按圖片按比例進行縮放,android貌似隻提供了一個android.graphics.Bitmap類來進行圖片處理,所有的格式(png jpg gif)都可以用Bitmap來表示和存儲。
代碼:
public voidWriteFileEx() {
try {
//獲取源圖片的大小
Bitmap bm;
BitmapFactory.Optionsopts = new BitmapFactory.Options();
//當opts不為null時,但decodeFile返回空,不為圖片分配內存,隻獲取圖片的大小,並保存在opts的outWidth和outHeight
BitmapFactory.decodeFile("/sdcard/Stephen.jpg"opts);
intsrcWidth = opts.outWidth;
intsrcHeight = opts.outHeight;
intdestWidth = 0;
intdestHeight = 0;
//縮放的比例
doubleratio = 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.OptionsnewOpts = 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);
//獲取縮放後圖片
BitmapdestBm = BitmapFactory.decodeFile("/sdcard/Stephen.jpg" newOpts);
if(destBm== null) {
showAlert("CreateFile"0 "Create Failed" "OK" false);
} else {
//文件命名,通過GUID可避免命名的重複
StringfileName = java.util.UUID.randomUUID().toString() + ".jpg";
//另外定義:
//ConfigManager.photoDir= getFileStreamPath(photoDirName)
//StringphotoDirName = "photo";要注意是根目錄
FiledestFile = new File(ConfigManager.photoDir fileName);
//創建文件輸出流
OutputStreamos = new FileOutputStream(destFile);
//存儲
destBm.compress(CompressFormat.JPEG100 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 voidsetImgView(String fileName) {
try
{
File file =new File(ConfigManager.photoDir fileName);
InputStreamis = 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 voidsetDrawable(String fileName) {
try {
File file =new File(ConfigManager.photoDir fileName);
InputStreamis = 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 voidsetDrawableAbsolute(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()的方法來獲取完整的絕對路徑來進行操作。
最後更新:2017-04-02 16:47:34