activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_from_album_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="相冊选取——返回值"/>
<Button
android:id="@+id/btn_from_album_uri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相冊选择——PATH(终极)"/>
<Button
android:id="@+id/btn_from_album_path_crop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从相冊并截取——PATH(终极)"/>
<ImageView
android:id="@+id/image_result"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"/>
</LinearLayout>package com.harvic.blogalbumcropfinally;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends Activity implements View.OnClickListener {
// 相冊选取,返回直接得到图片就可以
private static final int RESULT_ALBUM_ONLY_THROUGH_DATA = 200;
private static final int RESULT_ALBUM_ONLY_THROUGH_URI = 201;
private static final int RESULT_ALBUM_CROP_URI = 301;
private static final int RESULT_ALBUM_CROP_PATH = 302;
private static final int RESULT_CAMERA_CROP_PATH_RESULT = 401;
private Uri imageUri;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = getSDCardPath();
File file = new File(path + "/temp.jpg");
// 获取存放截图的路径信息
imageUri = Uri.fromFile(file);
Button btn_from_camera_crop = (Button) findViewById(R.id.btn_from_album_uri);
Button btn_from_pic = (Button) findViewById(R.id.btn_from_album_data);
Button btn_from_pic_path_crop = (Button) findViewById(R.id.btn_from_album_path_crop);
mImageView = (ImageView) findViewById(R.id.image_result);
btn_from_camera_crop.setOnClickListener(this);
btn_from_pic.setOnClickListener(this);
btn_from_pic_path_crop.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
// 通过intent.setData()来返回数据
case R.id.btn_from_album_data:
takeAlbumOnlyThroughData();
break;
// 通过找到图片的路径,生成相应的URI来显示图片
case R.id.btn_from_album_uri:
takeAlbumThroughURI();
break;
// 通过找到图片的路径,指定裁剪
case R.id.btn_from_album_path_crop:
takeAlbumCropPath();
break;
default:
break;
}
}
/**
* 通过相冊选取。直接返回得到相冊图片就可以
*/
private void takeAlbumOnlyThroughData() {
// 启动相冊的ACTION为ACTION_GET_CONTENT
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
startActivityForResult(intent, RESULT_ALBUM_ONLY_THROUGH_DATA);
}
/**
* 通过找到图片的路径,生成相应的URI来显示图片
*/
private void takeAlbumThroughURI() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
startActivityForResult(intent, RESULT_ALBUM_ONLY_THROUGH_URI);
}
/**
* 从相冊并截取——PATH(终极)
*/
private void takeAlbumCropPath() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
startActivityForResult(intent, RESULT_ALBUM_CROP_PATH);
}
public void cropImg(Uri uri) {
File tempFile = getTempFile();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 700);
intent.putExtra("outputY", 700);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, RESULT_CAMERA_CROP_PATH_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
/**
* 相冊选取,直接返回就可以
* 这里没有将return-data设为false,也就是使用了默认值。即直接使用Intent中的data域来传递结果值
*/
case RESULT_ALBUM_ONLY_THROUGH_DATA: {
// 照片的原始资源地址
try {
// 能够从一个已知的图片Uri中获得图片的bitmap对象---
Bitmap photo = MediaStore.Images.Media.getBitmap(
getContentResolver(), data.getData());
if (photo != null) {
// 压缩处理
Bitmap smallBmp = setScaleBitmap(photo, 2);
mImageView.setImageBitmap(smallBmp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
/**
* 通过找到图片的路径。生成相应的URI来显示图片
*/
case RESULT_ALBUM_ONLY_THROUGH_URI: {
try {
// 使用ContentProvider通过URI获取原始图片
String picPath = parsePicturePath(MainActivity.this,
data.getData());
File file = new File(picPath);
// 获取相机图片的Uri地址信息
Uri uri = Uri.fromFile(file);
// 获取图片
Bitmap photo = MediaStore.Images.Media.getBitmap(
getContentResolver(), uri);
if (photo !=原文:http://www.cnblogs.com/zsychanpin/p/6906096.html