从手机中选择照片这是几乎所有应用的功能之一,主要考虑到一点的就是如果图片太大了,可能会OOM,简单的处理就是对图片进行压缩!
<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/photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择照片" />
<ImageView
android:id="@+id/photo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="选择自己相册照片"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
-----------操作代码--------------
public class PhotoActivity extends Activity {
/**点击选择照片按钮*/
private Button photoBtn;
/**选择后展示照片*/
private ImageView photoIv;
/**请求码*/
private final static int SELECT_PHOTO_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
initViews();
initListners();
}
private void initViews() {
photoBtn = (Button) findViewById(R.id.photo_btn);
photoIv = (ImageView) findViewById(R.id.photo_iv);
}
private void initListners() {
photoBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectPhotoFromAblum();
}
});
}
/**从相册库中选择图片操作*/
private void selectPhotoFromAblum() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, SELECT_PHOTO_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PHOTO_CODE:
if (null != data) {
/**取得图片,图片压缩比为4*4*/
Bitmap bm = compressBitmap(null, null, this, data.getData(), 4, false);
photoIv.setImageBitmap(bm);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**图片压缩处理,size参数为压缩比,比如size为2,则压缩为1/4**/
private Bitmap compressBitmap(String path, byte[] data, Context context, Uri uri, int size, boolean width) {
Options options = null;
if (size > 0) {
Options info = new Options();
/**如果设置true的时候,decode时候Bitmap返回的为数据将空*/
info.inJustDecodeBounds = false;
decodeBitmap(path, data, context, uri, info);
int dim = info.outWidth;
if (!width) dim = Math.max(dim, info.outHeight);
options = new Options();
/**把图片宽高读取放在Options里*/
options.inSampleSize = size;
}
Bitmap bm = null;
try {
bm = decodeBitmap(path, data, context, uri, options);
}
catch (Exception e) {
e.printStackTrace();
}
return bm;
}
/**把byte数据解析成图片*/
private Bitmap decodeBitmap(String path, byte[] data, Context context, Uri uri, BitmapFactory.Options options) {
Bitmap result = null;
if (path != null) {
result = BitmapFactory.decodeFile(path, options);
}
else if (data != null) {
result = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
else if (uri != null) {
ContentResolver cr = context.getContentResolver();
InputStream inputStream = null;
try {
inputStream = cr.openInputStream(uri);
result = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
最后注意权限:
<!-- 从SDCard读取数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>