得到图片:方法一:本地选取图片,剪裁出需要那部分,方法二:打开摄像头照相,把照片选取需要部分
上传到服务器,更新本地头像:
代码如下:
public class AvaterMainActivity extends Activity {
private static int CAMERA_REQUEST_CODE = 1;
private static int GALLERY_REQUEST_CODE = 2;
private static int CROP_REQUEST_CODE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity_main);
Button btn_camera = (Button)findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (hasBackFacingCamera() || hasFrontFacingCamera() ) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
else {
ToastManager.getInstance(AvaterMainActivity.this).show("没有找到摄像头");
}
}
});
Button btn_gallery = (Button)findViewById(R.id.btn_gallery);
btn_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_REQUEST_CODE);
}
});
}
private Uri saveBitmap(Bitmap bm)
{
File tmpDir = new File(Environment.getExternalStorageDirectory() + "/com.zhangzhengfa.avater");
if(!tmpDir.exists())
{
tmpDir.mkdir();
}
File img = new File(tmpDir.getAbsolutePath() + "avater.png");
try {
FileOutputStream fos = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
fos.flush();
fos.close();
return Uri.fromFile(img);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private Uri convertUri(Uri uri)
{
InputStream is = null;
try {
is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return saveBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private void startImageZoom(Uri uri)
{
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST_CODE)
{
if(data == null)
{
return;
}
else
{
Bundle extras = data.getExtras();
if(extras != null)
{
Bitmap bm = extras.getParcelable("data");
Uri uri = saveBitmap(bm);
startImageZoom(uri);
}
}
}
else if(requestCode == GALLERY_REQUEST_CODE)
{
if(data == null)
{
return;
}
Uri uri;
uri = data.getData();
Uri fileUri = convertUri(uri);
startImageZoom(fileUri);
}
else if(requestCode == CROP_REQUEST_CODE)
{
if(data == null)
{
return;
}
Bundle extras = data.getExtras();
if(extras == null){
return;
}
Bitmap bm = extras.getParcelable("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bm);
sendImage(bm);
}
}
//
// 向服务器传入图像
private void sendImage(Bitmap bm)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 60, stream);
byte[] bytes = stream.toByteArray();
String img = new String(Base64.encodeToString(bytes, Base64.DEFAULT));
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("img", img);
client.post("http://192.168.56.1/ImgUpload.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
Toast.makeText(MainActivity.this, "Upload Success!", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
Toast.makeText(MainActivity.this, "Upload Fail!", Toast.LENGTH_LONG).show();
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") private boolean checkCameraFacing( int facing) {
if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
return false;
}
final int cameraCount = Camera.getNumberOfCameras();
CameraInfo info = new CameraInfo();
for (int i = 0; i < cameraCount; i++) {
Camera.getCameraInfo(i, info);
if (facing == info.facing) {
return true;
}
}
return false;
}
public boolean hasBackFacingCamera() {
final int CAMERA_FACING_BACK = 0;
return checkCameraFacing(CAMERA_FACING_BACK);
}
public boolean hasFrontFacingCamera() {
final int CAMERA_FACING_BACK = 1;
return checkCameraFacing(CAMERA_FACING_BACK);
}
public static int getSdkVersion() {
return android.os.Build.VERSION.SDK_INT;
}
}
相应xml文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="300px"
android:layout_height="300px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btn_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="摄像头" />
<Button
android:id="@+id/btn_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_camera"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:text="图库" />
</RelativeLayout>