调用系统的Camera
1.这只是简单的调用一下摄像头,然后拍一张照片。这个命令是基于Intent的,需要在setAction中添加调用摄像头的语句,然后利用Intent.putExtra将得到的照片存放到file里
最后复写onActivityResult,将拍下来的照片显示到ImageView上
2.需要权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
3.有的手机需要不能将所照的图片显示在ImageView上,是因为所照的照片比较大,需要将其压缩,就可以正常显示了,不过这里的压缩方法是在原文件的基础上进行的压缩,压缩后的文件会替换掉原文件
调用手机相册
1.从相册中选择图片,需要创建一个Intent对象,将他的action设置为Intent.ACTION_GET_CONTENT,然后设置类型为“iamge/*”,最后调用startActivityForResult()方法,就可以打开相册程序选择照片
2.在onActivityResult方法中,利用data来得到所选择的照片,从而将照片显示到ImageView上去,这里就不能用压缩了,因为这个压缩方法会将原来的图片替换为压缩过后的图片,因此不建议使用该压缩方法
代码实现
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mBtnCamera;
private Button mBtnPicture;
private ImageView mImage;
private File file;
public static final int CAMERA=0x88;
public static final int PICTTURE=0x66;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImage = (ImageView) findViewById(R.id.image_camera);
mBtnCamera = (Button) findViewById(R.id.button_camera);
mBtnPicture = (Button) findViewById(R.id.button_picture);
mBtnCamera.setOnClickListener(this);
mBtnPicture.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_camera:
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//Standard Intent action that can be sent to have the camera application capture an image and return it.
file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");//创建一个文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//将拍下来的照片,存放到file路径下
startActivityForResult(intent, CAMERA);
break;
case R.id.button_picture:
// Intent intentPicture=new Intent("android.intent.action.GET_CONTENT");
Intent intentPicture=new Intent(Intent.ACTION_GET_CONTENT);
intentPicture.setType("image/*");
startActivityForResult(intentPicture,PICTTURE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判断requestCode和resultCode是否是符合情况,如果符合就将拍下来的照片显示在ImageView上
if (resultCode==RESULT_OK){
switch (requestCode){
case CAMERA:
ImageZip.zipImage(file.getAbsolutePath());
mImage.setImageURI(Uri.fromFile(file));
break;
case PICTTURE:
Uri uri=data.getData();
mImage.setImageURI(uri);
break;
}
}
}
}
压缩照片代码
在源文件的基础上进行压缩,替换掉源文件
public class ImageZip {
public static void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}