图片过大处理

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;
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值