package com.stu.activity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.maizi.activity.R;
public class MainActivity extends Activity {
Button carmBtn;
ImageView photo;
int PICTURE_GET = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
carmBtn = (Button) findViewById(R.id.bt_carmer);
photo = (ImageView) findViewById(R.id.iv_photo);
carmBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICTURE_GET);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICTURE_GET && data != null) {
Uri uri = data.getData();
String imagePath = getAbsoluteImagePath(this, uri);
Bitmap bitmap = null;
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 700*780);
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imagePath, opts);
photo.setImageBitmap(bitmap);
} catch (OutOfMemoryError e) {// 这个异常是可以捕捉的,习惯性写上去
Toast.makeText(MainActivity.this, "图片太大", Toast.LENGTH_SHORT).show();
return;
}
}
}
// 获取图片的路径
public static String getAbsoluteImagePath(Activity mContext, Uri uri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = mContext.managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String res = cursor.getString(column_index);
try {
// 4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
if (Integer.parseInt(Build.VERSION.SDK) < 14) {
cursor.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
*
* @date 2014-6-8 下午11:31:25 Android源码动态算缩小值
*/
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
public static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
图片过大处理
最新推荐文章于 2020-10-19 18:39:44 发布