拍照,压缩
压缩程序:
package com.example.administrator.mymediasurfaceview.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Administrator on 2015/9/16.
*/
public class ZipImage {
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;
}
public 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;
}
}
}
ZipImage.zipImage(file.getAbsolutePath());
mImageView.setImageURI(Uri.fromFile(file));//得到图片
最后注意权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在相册中选取相片
这是总的程序,包括拍照和相册中选取照片的,其中case 0x23:是在相册中选取照片的。
package com.example.administrator.mymediasurfaceview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.example.administrator.mymediasurfaceview.utils.ZipImage;
import java.io.File;
import java.io.IOException;
/**
* Created by Administrator on 2015/9/15.
*/
public class CameraActivity extends Activity {
private Button mButtonStartCamera;
private Button mButtonGetGallery;
private ImageView mImageView;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mButtonStartCamera = (Button) findViewById(R.id.button_start_camera);
mButtonGetGallery = (Button) findViewById(R.id.button_get_gallery);
mImageView = (ImageView) findViewById(R.id.imageview);
mButtonGetGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,0x24);
}
});
mButtonStartCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//告诉系统相机将照片保存的位置
startActivityForResult(intent,0x23);//开始启动
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
switch (requestCode){
case 0x23:
ZipImage.zipImage(file.getAbsolutePath());
mImageView.setImageURI(Uri.fromFile(file));//得到图片
break;
case 0x24:
Uri uri = data.getData();
mImageView.setImageURI(uri);
break;
}
}
}
}