手机图片的获取
来源:互联网 发布:Mac 变速播放器 编辑:程序博客网 时间:2024/06/10 19:39
PI Level 3 required!
We will start the operation on a buttons onclick event, implemented as follows:
- private static Bitmap Image = null;
- private static Bitmap rotateImage = null;
- private ImageView imageView;
- private static final int GALLERY = 1;
- public void onClick(View v) {
- imageView.setImageBitmap(null);
- if (Image != null)
- Image.recycle();
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
- }
In order to avoid out of memory errors, we must recycle the previous image when the user pressesthe button second times or after that, so the returned bitmap is stored as a member variable, so we still have a reference for it when it is needs to be recycled.
We process the result in the onActivityResult method, which is called automatically when the gallery is finished.
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == GALLERY && resultCode != 0) {
- Uri mImageUri = data.getData();
- try {
- Image = Media.getBitmap(this.getContentResolver(), mImageUri);
- if (getOrientation(getApplicationContext(), mImageUri) != 0) {
- Matrix matrix = new Matrix();
- matrix.postRotate(getOrientation(getApplicationContext(), mImageUri));
- if (rotateImage != null)
- rotateImage.recycle();
- rotateImage = Bitmap.createBitmap(Image, 0, 0,Image.getWidth(), Image.getHeight(), matrix,true);
- imageView.setImageBitmap(rotateImage);
- } else
- imageView.setImageBitmap(Image);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
The getOrientation method, used above returns the angle the image was taken. So if it was made with a rotated phone we must rotate the image before we can correctly display it in an ImageView. It can me implemented with the help of the Android MediaStore.
- public static int getOrientation(Context context, Uri photoUri) {
- Cursor cursor = context.getContentResolver().query(photoUri,
- new String[] { MediaStore.Images.ImageColumns.ORIENTATION},null, null, null);
- if (cursor.getCount() != 1) {
- return -1;
- }
- cursor.moveToFirst();
- return cursor.getInt(0);
- }
- 手机图片的获取
- android 扫描手机上的图片 获取手机图片路径
- 获取手机图片和视频的缩略图
- Android获取手机图片路径的办法
- 获取手机中最新的图片
- 获取手机中的图片
- Android 获取手机图片
- 获取手机所有图片
- 获取手机刚拍照的图片和获取手机内所有图片
- 获取Android手机的图片和视频的缩略图
- 手机拍照获取不到图片?<部分手机>
- 手机图片获取、裁剪、上传
- android获取手机所有图片
- 获取Android手机上的图片和视频缩略图
- 如何获取手机和存储卡上的图片
- 获取Android手机上的图片和视频缩略图
- 获取Android手机上的图片和视频缩略图
- android获取手机上的图片和视频缩略图thumbnails
- Reverse Words in a String
- UVa 10253 Series-Parallel Networks
- 各种编码UNICODE、UTF-8、ANSI、ASCII、GB2312、GBK详解
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 手机图片的获取
- hadoop HDFS文件系统的特征
- HDU1867 A + B for you again KMP应用
- Hive "行转列"和"列转行"
- mabatis入门
- 关于为何子类的构造器必须调用父类的构造器一次的解答
- UVa 11610 Reverse Prime(树状数组+二分)
- TIME_WAIT状态详解
- XML配置