參考自《第一行代碼》及相關資料,這裏存一遍後以後就不用再重頭打一遍了。。
首先由於進行讀寫操作,要在 AndroidManifest.xml中聲明權限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
調用系統相冊:
private static final int CHOOSE_PHOTO=0;
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
然後回調:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CHOOSE_PHOTO:
if (resultCode == RESULT_OK) {
Bitmap bitmap = null;
//判斷手機系統版本號
if (Build.VERSION.SDK_INT >= 19) {
//4.4及以上系統使用這個方法處理圖片
bitmap = ImgUtil.handleImageOnKitKat(this, data); //ImgUtil是自己實現的一個工具類
} else {
//4.4以下系統使用這個方法處理圖片
bitmap = ImgUtil.handleImageBeforeKitKat(this, data);
}
ImageView view = (ImageView) findViewById(R.id.personal_info_header_img);
view.setImageBitmap(bitmap);
}
break;
default:
break;
}
}
將對圖像的相關操作封裝成一個ImgUtil類,便於使用:
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* Created by wbin on 2016/3/22.
*/
public class ImgUtil {
//4.4及以上系統使用這個方法處理圖片
@TargetApi(19)
public static Bitmap handleImageOnKitKat(Context context, Intent data) {
String imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(context, uri)) {
//如果是document類型的Uri,則通過document id處理
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1]; //解析出數字格式的id
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getImagePath(context, contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
//如果不是document類型的Uri,則使用普通方式處理
imagePath = getImagePath(context, uri, null);
}
return getImage(imagePath);
}
//4.4以下系統使用這個方法處理圖片
public static Bitmap handleImageBeforeKitKat(Context context, Intent data) {
Uri uri = data.getData();
String imagePath = getImagePath(context, uri, null);
return getImage(imagePath);
}
public static String getImagePath(Context context, Uri uri, String selection) {
String path = null;
//通過Uri和selection來獲取真實的圖片路徑
Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
//對bitmap進行質量壓縮
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { //循環判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,把壓縮後的數據存放到baos中
options -= 10;//每次都減少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的數據baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數據生成圖片
return bitmap;
}
//傳入圖片路徑,返回壓縮後的bitmap
public static Bitmap getImage(String srcPath) {
if (TextUtils.isEmpty(srcPath)) //如果圖片路徑爲空 直接返回
return null;
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此時返回bm爲空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//現在主流手機比較多是800*480分辨率,所以高和寬我們設置爲
float hh = 800f;//這裏設置高度爲800f
float ww = 480f;//這裏設置寬度爲480f
//縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可
int be = 1;//be=1表示不縮放
if (w > h && w > ww) {//如果寬度大的話根據寬度固定大小縮放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的話根據寬度固定大小縮放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//設置縮放比例
//重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);//壓縮好比例大小後再進行質量壓縮
}
}
爲了兼容新老版本的手機,我們做了一個判斷,如果是4.4及以上系統的手機就調用handleImageOnKitKat()方法來處理圖片,否則就調用handleImageBeforeKitKat()方法來處理圖片。之所以要這麼做,是因爲Android系統從4.4版本開始,選取相冊的圖片不再返回圖片真是的Uri了,而是一個封裝過的Uri,因此如果是4.4版本以上的手機需要對這個Uri進行解析纔行。
當然了,獲取到圖片路徑後不推薦直接使用 BitmapFactory.decodeFile(imgPath)來獲取bitmap,因爲某些圖片體積可能很大,直接加載到內存中有可能會導致程序崩潰(我就遇到過了..你可以直接加載手機高像素拍的原圖片試試看=。=)。 所以更好的做法是先對圖片進行適當的壓縮,然後再加載到內存中(上述代碼中實現了)。
好了,以後需要直接來這複製就行了,不用再蛋疼打這麼多了=。= 懶人福利。