android 上图片选取器以及图片尺寸太大情况下处理
2012-06-04 20:42
323 查看
代码请见我的资源http://download.csdn.net/detail/samguoyi/4351467
package test.sam.imgpickdemo; import java.io.File; import javax.security.auth.PrivateCredentialPermission; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.Camera; import android.media.ExifInterface; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; public class PickPictureDemoActivity extends Activity implements View.OnClickListener { private static final int ACTIVITY_PICKLOCAL = 0; private static final int ACTIVITY_PICKCAMERA = 1; private ImageView showImageView = null; private File imgFile = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.main_pickcamera).setOnClickListener(this); findViewById(R.id.main_picklocal).setOnClickListener(this); showImageView = (ImageView)findViewById(R.id.main_showimg); } public void onClick(View v) { switch (v.getId()) { case R.id.main_picklocal: /* * 使用这种intent则调用任何注册过的图片浏览器,例如es文件浏览器,来选取图片 */ Intent i = new Intent(); i.setType("image/*"); i.setAction(Intent.ACTION_GET_CONTENT); /* * 使用这种方式只调用系统的图库程序来选取图片 */ //Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, ACTIVITY_PICKLOCAL); break; case R.id.main_pickcamera: imgFile = new File(Environment.getExternalStorageDirectory(), "testpick"+System.currentTimeMillis()+".jpg"); try { imgFile.createNewFile(); } catch (Exception e) { } if(imgFile.exists()) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile)); startActivityForResult(intent, ACTIVITY_PICKCAMERA); } break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); System.out.println("resultCode = "+resultCode); String photopathString = null; if(resultCode == RESULT_OK) { if(requestCode == ACTIVITY_PICKLOCAL) { Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null); cursor.moveToFirst(); photopathString = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); Bitmap bitmap = BitmapFactory.decodeFile(photopathString); showImageView.setImageBitmap(bitmap); cursor.close(); /* * 这段代码可以获取图片的方向,以决定图片是横向放置还是竖向放置 */ try { ExifInterface exifInterface = new ExifInterface(photopathString); String tag = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION); System.out.println("tag = "+tag); } catch (Exception e) { e.printStackTrace(); } /* * 这段代码可以将图片的长宽等信息decode出来而不需要分配内存 */ try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(photopathString, options); System.out.println("options.outWidth = "+options.outWidth); System.out.println("options.outHeight = "+options.outHeight); } catch (Exception e) { } /* * 使用这段代码可以缩小取得图片尺寸options.inSampleSize为几,则取得的图片大小为原来的几分之一 * 注意inSampleSize需要为2的幂次方 */ try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap bitmap2 = BitmapFactory.decodeFile(photopathString, options); System.out.println("bitmap2.getWidth() = "+bitmap2.getWidth()); System.out.println("bitmap2.getHeight() = "+bitmap2.getHeight()); } catch (Exception e) { } } if(requestCode == ACTIVITY_PICKCAMERA) { /* * 从摄像头返回的图片无法从data.getData()中取得?不知道为啥 */ Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); System.out.println(bitmap.getWidth()); System.out.println(bitmap.getHeight()); showImageView.setImageBitmap(bitmap); } } } }
相关文章推荐
- Android 选取图片、手机拍照以及保存图片到本地、读取图片
- Android 拍照以及一些常用的处理,例如将图片显示到相册(包含了安卓系统6.0以上调用相机的处理)
- Android之本地相册图片选取和拍照以及图片剪辑
- Android之本地相冊图片选取和拍照以及图片剪辑
- Android腾讯微薄客户端开发七:图片加圆角以及时间处理工具类
- android中Bitmap图像处理 修改图片大小以及保存时的文件大小
- android 里面对图片的不同压缩处理 以及简单拍照功能实现加设置闪光灯效果
- 如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片
- H5页面打开android本地照册,当webview页面被系统回收的情况下,如何处理图片
- android中Bitmap图像处理 修改图片大小以及保存时的文件大小
- Android仿微信,上传本地多张图片,缩放预览,以及图片处理等
- Android图片质量压缩和尺寸压缩处理
- android GridView显示相同尺寸图片以及预览功能
- android获取图片尺寸的两种方法以及bitmap的缩放
- Android图片缩放、圆角处理以及倒影特效代码
- Android图片缩放、圆角处理以及倒影特效代码
- android 计算Bitmap占byte大小,以及Bitmap中像素数的计算,另外再给出一个按尺寸裁剪图片的方法
- android 大尺寸图片处理
- Android 图片处理以及recycle机制
- Android 图片加圆角以及时间处理工具类