目標:從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()的方法來擷取完整的絕對路徑來進行操作。