呼叫系統相機、相簿並選擇圖片
相信很多學習android的朋友,都和我一樣連一個簡單的獲取手機圖片的無從知曉,於是藉助於百度尋求幫助,於是乎有感寫,這篇部落格不僅僅是鞏固自身的基礎知識,希望能幫助到其他人。 Uri mPhotoUri; private static final int REQUESTCODE_PICK = 0; // 相簿選圖示記 private static final int REQUESTCODE_TAKE = 1; // 相機拍照標記 //如果不太明白Uri //請檢視連結:http://blog.csdn.net/sunny09290/article/details/7514963 1.呼叫系統相機 Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPURE)
//獲取儲存路徑
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mPhotoUri);//可以忽略
startActivityForResult(intent,REQUESTCODE_TAKE);//跳轉相機
//從圖片庫獲取圖片 Intent intent; if (Build.VERSION.SDK_INT < 19) { intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); } else { intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } startActivityForResult(intent, REQUESTCODE_PICK);
//重寫回調方法
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {//是否選中圖片
switch (requestCode) {
case REQUESTCODE_PICK:// 直接從相簿獲取
Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close();
// Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Bitmap bitmap = FileUtil.getSmallBitmap(picturePath);//壓縮圖片
setPotoFeedImageItem(bitmap);
break;
case REQUESTCODE_TAKE:// 呼叫相機拍照
if (resultCode == Activity.RESULT_OK) {
final Uri uri = mPhotoUri;
Log.e("resultCode", uri + "");
if (uri != null) {
processPicture(uri);
}
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 圖片顯示
*
* @param uri
*/
private void processPicture(Uri uri) {
final String[] projection = {MediaStore.Images.Media.DATA};
final Cursor cursor = managedQuery(uri, projection, null, null, null);
cursor.moveToFirst();
final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String imagePath = cursor.getString(columnIndex);
Debug.e("processPicture", imagePath);
Bitmap bitmap = FileUtil.getSmallBitmap(imagePath);//壓縮圖片
setPotoFeedImageItem(bitmap);
}
//將圖片顯示在控制元件上
privet void setPotoFeedImageItem(Bitmap bitmap){
ImageView acatarimg = (ImageView) findViewById(R.id.img);
acatarimg.setImageBitmap(bitmap);
}
下面的是一個壓縮圖片的工具類
public class FileUtil {
/**
* 裁剪圖片方法實現
*
* @param uri
*/
public static void startPhotoZoom(Activity cot, Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop=true是設定在開啟的Intent中設定顯示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
cot.startActivityForResult(intent, REQUESTCODE_CUTTING);
}
//計算圖片的縮放值
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
// 根據路徑獲得圖片並壓縮,返回bitmap用於顯示
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
}