您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: