編輯:關於Android編程
先上效果圖:

Layout為:
private class LoadPhotoTask extends AsyncTask{ private Bitmap mLoadedBitmap = null; protected Boolean doInBackground(Void... params) { try { if(mFilePathName.length() > 0){ File file = new File(mFilePathName); if (!file.exists()) { return false; } BitmapFactory.Options opt = new BitmapFactory.Options(); long fileSize = file.length(); int maxSize = 2*1024 * 1024; if(fileSize <= maxSize){ opt.inSampleSize = 1; }else if(fileSize <= maxSize * 4){ //小於8M opt.inSampleSize = 2; }else{ long times = fileSize / maxSize; opt.inSampleSize = (int)(Math.log(times) / Math.log(2.0)) + 1; //Math.log返回以e為底的對數 } try{ mLoadedBitmap = BitmapFactory.decodeFile(mFilePathName,opt);//opt為縮小的倍數 mUploadFilePathName = SaveBitmapToFile(mLoadedBitmap); }catch(OutOfMemoryError e){ Toast.makeText(UploadPhotoActivity.this, getResources().getString(R.string.no_memory_to_view_photo), Toast.LENGTH_SHORT).show(); UploadPhotoActivity.this.finish(); } } return true; } catch (Exception e) { Log.e("UploadPhotoActivity", "doInBackground", e); return false; } } protected void onPostExecute(Boolean result){ try { showLoadPreviewProgressBar(false); if(mLoadedBitmap != null){ ImageView IamgePreView = (ImageView)findViewById(R.id.photo_upload_preview_image); IamgePreView.setImageBitmap(mLoadedBitmap); }else{ } mLoadedBitmap = null; } catch (Exception e) { Log.e("UploadPhotoActivity", "onPostExecute", e); } } }
private String SaveBitmapToFile(Bitmap bmp){
if (null == bmp) {
return null;
}
String fileName = "upload_tmp.jpg";
File f = this.getFileStreamPath(fileName);//data/data/com.example.tianqitongtest/files/upload_tmp.jpg,這個是要上傳的文件存的位置
if (f.exists()) {
f.delete();
}
FileOutputStream ostream;
try {
int targetWidth = 780;
int w = bmp.getWidth();
if (w > targetWidth) {
int h = bmp.getHeight();
int targetHeight = (targetWidth * h) / w;
bmp = Bitmap.createScaledBitmap(bmp, targetWidth, targetHeight,
true);//根據指定寬度和高度來生成一個新的Bitmap
}
ostream = this.openFileOutput(fileName, MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.JPEG, 70, ostream);
ostream.flush();
ostream.close();
ostream = null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f.getAbsolutePath();
}private void hideInputMethod(){
View view = getCurrentFocus();
if(view != null){
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
private String getStoredPicPath() {
String fileName = "11.jpg";
return Environment.getExternalStorageDirectory()+"/"+fileName;
}
Android Zipalign工具優化Android APK應用
生成的Android應用APK文件最好進行優化,因為APK包的本質是一個zip壓縮文檔,經過優化
Android自定義照相機詳解
幾乎每個APP都會用的相機功能,下面小編把內容整理分享到本站平台,供大家參考,感興趣的朋友一起學習吧!啟動相機的兩種方式1.直接啟動系統相機<code class=
[Android開發系列]IT博客應用
1.關於坑 好吧,在此之前先來說一下,之前開的坑,恩,確實是坑,前面開的兩個android開發教程的坑,對不起,實在是沒什麼動力了,不過源碼都有的,大家可以參照githu
Android控件之ListView用法實例詳解
本文實例講述了Android控件之ListView用法。分享給大家供大家參考。具體如下:示例一:在android開發中ListView是比較常用的組件,它以列表的形式展示