Android 选取照片
涉及的问题:
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的问题,是由于图片文件过大,导致内存吃紧,然后就考虑压缩这个图片(其实是只加载一部分,在图片文件中图片还是那个图片,不增不减)。这样选取系统的图片的目的就达到了。
报名学习加微信/QQ 1602007,关注《东方联盟网》微信公众号
>更多相关文章
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 03-08消息称微软开发内部AI推理模型,或将成为Op
- 03-08美国法院驳回马斯克请求,未阻止OpenAI转型
- 03-08饿了么成立即时配送算法专家委员会 持续全局
- 03-08长安汽车:预计今年底长安飞行汽车将完成试
- 03-08谷歌推出虚拟试穿、AR美妆新功能
相关文章
24小时热门资讯
热门推荐
最新资讯
操作系统
黑客防御