Android 选取照片
2013-12-06 16:00
302 查看
涉及的问题:
1.怎么选取?
2.选到怎么应用?
------
回答怎么选取的问题:
通常有两种常见的额方法:拍摄新照片和从已有的图库中选取图片
1.1拍摄新照片调用相机,参考:http://developer.android.com/guide/topics/media/camera.html#intents
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的问题,是由于图片文件过大,导致内存吃紧,然后就考虑压缩这个图片(其实是只加载一部分,在图片文件中图片还是那个图片,不增不减)。这样选取系统的图片的目的就达到了。
《完》
参考
http://blog.csdn.net/ryantang03/article/details/8654137 http://www.oschina.net/question/157182_53236
1.怎么选取?
2.选到怎么应用?
------
回答怎么选取的问题:
通常有两种常见的额方法:拍摄新照片和从已有的图库中选取图片
1.1拍摄新照片调用相机,参考:http://developer.android.com/guide/topics/media/camera.html#intents
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的问题,是由于图片文件过大,导致内存吃紧,然后就考虑压缩这个图片(其实是只加载一部分,在图片文件中图片还是那个图片,不增不减)。这样选取系统的图片的目的就达到了。
《完》
参考
http://blog.csdn.net/ryantang03/article/details/8654137 http://www.oschina.net/question/157182_53236
相关文章推荐
- Androd选取相册照片和拍照处理-android学习之旅(62)
- Android调用相册或相机拍照选取照片并裁剪
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Androd选取相册照片和拍照处理-android学习之旅(62)
- Android中照相,从相册选取照片,压缩,保存到手机内存,展示到界面,点击放大,上传。(含demo)
- android菜瓜笔记之手机照片选取与拍摄
- Android 从相册选取多张照片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android--从相册中选取照片并返回结果
- android选取照片并以其uri取得绝对路径
- Android——UI篇:WebView里调用相机/文件选取照片并上传。
- Android完美满足你所有的拍照或从相册中选取照片
- Android 拍照,选取照片,截取,显示
- Android 调用相机拍摄照片或者从相册选取照片
- android选取照片后做处理
- H5调用手机的摄像头拍照上传以及手机相册选取照片但不支持Android的分析
- android选取照片并以其uri取得绝对路径
- Android从相册以及拍照选取照片,然后裁剪保存遇到的问题
- Android 实现调用照相机和选取本地照片功能