String ACTION_IMAGE_CAPTURE Standard Intent action that can be sent to have the camera application capture an image and return it.
String MEDIA_MOUNTED getExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access.
媒体就绪
static File getExternalStorageDirectory()
Gets the Android external storage directory.
Environment.getExternalStorageDirectory() = = /storage/sdcard0
String EXTRA_OUTPUT The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video.
case R.id.camara:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File fileDir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
fileDir = new File(Environment.getExternalStorageDirectory()+"/user_photos");
if(!fileDir.exists()){
fileDir.mkdirs();
}
}else{
Toast.makeText(this, R.string.sd_noexit, Toast.LENGTH_SHORT);
return;
}
cameraFile = new File(fileDir.getAbsoluteFile()+"/"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
startActivityForResult(intent, REQUE_CODE_CAMERA);
break;
startActivityForResult( )和 onActivityResult( )形成对应关系
onActivityResult
public void startActivityForResult (Intent intent, int requestCode)
protected void onActivityResult (int requestCode, int resultCode, Intent data)
requestCode值与resultCode值对应
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUE_CODE_CAMERA:
startPhotoZoom(Uri.fromFile(cameraFile));
break;
case REQUE_CODE_PHOTO:
if(null!= data){
startPhotoZoom(data.getData());
}
break;
case REQUE_CODE_CROP:
cropBitmap = data.getParcelableExtra("data");
if(cropBitmap!= null){
view_image.setImageBitmap(cropBitmap);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
剪裁图片处理方法
public Intent setDataAndType (Uri data, String type)
Parameters
data The URI of the data this intent is now targeting.
type The MIME type of the data being handled by this intent.
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUE_CODE_CROP);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUE_CODE_CROP:
//从返回的Intent中获取数据
cropBitmap = data.getParcelableExtra("data");
if(cropBitmap!= null){
view_image.setImageBitmap(cropBitmap);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
将图片压缩成字节数组数据,并通过socket输出
//将Bitmap类型图片数据压缩进入字节数组输出流byteoutput
ByteArrayOutputStream byteoutput = new ByteArrayOutputStream();
cropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteoutput);
//将字节数组输出流转化成字节数组
byte datas[] = byteoutput.toByteArray();
DataOutputStream output = null;
Socket socket = null;
try {
SocketAddress socAddress = new InetSocketAddress(InetAddress.getByName(ip), Integer.parseInt(port));
socket = new Socket();
socket.connect(socAddress, 3000);
output = new DataOutputStream(socket.getOutputStream());
output.writeInt(datas.length);
output.write(datas);
if (cropBitmap != null) cropBitmap.recycle();
} catch (IOException e) {
throw new IOException("fail connect to the server");
} finally {
try {
if(byteoutput!= null)
byteoutput.close();
if (output != null){
output.flush();
output.close();
}
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
将图片数据存入文件
File fileDir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
fileDir = new File(Environment.getExternalStorageDirectory()+"/userImage");
}else{
fileDir = new File(context.getFilesDir()+"/userImage");
}
if(!fileDir.exists()) fileDir.mkdirs();
String fileName = System.currentTimeMillis()+".png";
File imageFile = new File(fileDir.getAbsoluteFile()+"/"+fileName);
OutputStream output = new FileOutputStream(imageFile);
Bitmap bitmap = user.getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, output);