Android 选取照片

浏览:
字体:
发布时间:2013-12-09 23:24:00
来源:

涉及的问题:

1.怎么选取?

2.选到怎么应用?

------

回答怎么选取的问题:

通常有两种常见的额方法:拍摄新照片和从已有的图库中选取图片

1.1拍摄新照片调用相机,

 

 Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);startActivityForResult(openCameraIntent, TAKE_NEW);

 

其中:

 

    Uri mImageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), image.jpg));

在onActivityResult 中做处理。

 

为了避免OOM,采取压缩bitmap的方式,在应用中设置一个大概的值,来作为压缩的标准。

注意,由于使用了openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); 在 onActivityResult

 

@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        int req = getResources().getDimensionPixelSize(R.dimen.record_event_image_size);        switch (requestCode) {            case TAKE_NEW:                if (resultCode == RESULT_OK) {                    File imageFile = new File(mImageUri.getPath());                    File destFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + .jpg);                    try {                        FileUtils.copyFile(imageFile,destFile);                    } catch (IOException e) {                    }                    addAddedImage(destFile,req);                }                break;                    }    }
其中addAddedImage 是处理的过程,回答了怎么使用的问题。

 

 

 private void addAddedImage(File selectedFile,int req){        final BitmapFactory.Options options = new BitmapFactory.Options();        Bitmap smallBitmap =  BitmapFactory.decodeFile(selectedFile.getAbsolutePath(),options);        int inSampleSize = CommonTool.calculateInSampleSize(options, req, req);        options.inSampleSize = inSampleSize;        options.inJustDecodeBounds = false;        smallBitmap = BitmapFactory.decodeFile(selectedFile.getAbsolutePath(),options);        ResourceView view = new ResourceView(mContext);        view.getImageView().setImageBitmap(smallBitmap);        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);        params.setMargins(10,10,10,10);        mResourceView.addView(view,0,params);    }
其中的提取是关键:

 

 

  public static int calculateInSampleSize(BitmapFactory.Options options,                                            int reqWidth, int reqHeight) {        // Raw height and width of image        final int height = options.outHeight;        final int width = options.outWidth;        int inSampleSize = 1;        if (height > reqHeight || width > reqWidth) {            // Calculate ratios of height and width to requested height and            // width            final int heightRatio = Math.round((float) height                    / (float) reqHeight);            final int widthRatio = Math.round((float) width / (float) reqWidth);            // Choose the smallest ratio as inSampleSize value, this will            // guarantee            // a final image with both dimensions larger than or equal to the            // requested height and width.            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;        }        return inSampleSize;    }
上面是参考的,具体的出处现在急不得了。

 

这样就完成了拍摄新照片并且使用的目的了。

1.2 从图库中选取照片

 

Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);                        openAlbumIntent.setType(image/*);                        startActivityForResult(openAlbumIntent, SELECT);

因为是从系统中选取的,而android的数据都是有contentresolver来进行提供的。

 

为了避免OOM,我们先将图片的文件路径给在找出来,然后使用上面提供的压缩方式连进行提取一个缩略图

方式如下:

 

 Uri selectedImage = data.getData();                    Toast.makeText(mContext, Selected:  + selectedImage, Toast.LENGTH_LONG).show();                    ContentResolver resolver = getContentResolver();                    String[] filePathColumn = { MediaStore.Images.Media.DATA };                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);                    cursor.moveToFirst();                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);                    String picturePath = cursor.getString(columnIndex);                    cursor.close();

注意使用cursor使用完成后要立马关闭。

 

得到了绝对的路径,处理方式就可以和上面的完全相同了。

--------------

总结:

选取图片的总体思路是,既然是图片,我们就认为它一定是存储在一个图片文件里面的(没有的话,我们可以强制的让他存在),然后在从文件中加载到内存中进行展现。在展现的过程中我们遇到了OOM的问题,是由于图片文件过大,导致内存吃紧,然后就考虑压缩这个图片(其实是只加载一部分,在图片文件中图片还是那个图片,不增不减)。这样选取系统的图片的目的就达到了。

 


 


 

>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();