图片过大处理
来源:互联网 发布:linux进程调度命令 编辑:程序博客网 时间:2024/04/29 21:34
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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction(Intent.ACTION_GET_CONTENT);intent.setType("image/*");startActivityForResult(intent, PICTURE_GET);}});}@Overrideprotected 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;}}}
0 0
- 图片过大处理
- android 上传图片过大处理
- android 上传图片过大处理
- android上传图片过大处理
- 对于背景图等过大图片的处理
- iOS图片消耗内存过大的处理
- UIWebView加载富文本处理图片宽度过大问题
- navigationBar图片过大
- 图片过大导致OOM
- android 图片过大如何处理?压缩后图片有多大? 分辨率压缩、质量压缩
- LOBSEGMENT过大的处理
- MFC显示图片过大,拖动滚动条时不正常处理方法
- 日志文件过大 处理方法
- log文件过大处理方法
- 怎样处理服务器负载量过大
- oracle11g 监听日志过大处理
- 监听日志过大处理指南
- div+css图片过大,隐藏图片
- swift入门
- 冒泡排序
- 编写小游戏《贪头蛇》第一篇
- 类图和对象图
- The requested list key 'map' could not be resolved as a collection/array/map/enumeration/iterator ty
- 图片过大处理
- lua学习之类型与值
- android 初级入门 intent
- eclipse导出jar包,不能加载@RequestMapping等类似注解的问题
- OOA与OOD的关系
- thrift下C++服务器和客户端开发
- implement a function of String randomAlphabetic(int count)
- CODE:BLOCKS不支持C++11
- 前言