activity_main.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"
tools:context="${packageName}.${activityClass}" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="300px"
android:layout_height="300px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
<Button
android:id="@+id/btn_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="拍照" />
<Button
android:id="@+id/btn_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_camera"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="图库选择" />
RelativeLayout>
MainActivity.java
package com.example.imagecutdemo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int CAMERA_REQUEST_CODE = 0;
protected static final int ALBUM_REQUEST_CODE = 1;
private static final int CROP_REQUEST_CODE = 2;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
Button btnCamera = (Button) findViewById(R.id.btn_camera);
btnCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
Button btnAlbum = (Button) findViewById(R.id.btn_album);
btnAlbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/**这里有2种获取相册的方式*/
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
//Intent intent = new Intent(Intent.ACTION_PICK, null);
//intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, ALBUM_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_CANCELED)
return;
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap bitmap = bundle.getParcelable("data");
Uri uri = saveBitmap(bitmap);
startImageZoom(uri);
}
} else if (requestCode == ALBUM_REQUEST_CODE) {
if (resultCode == Activity.RESULT_CANCELED)
return;
Uri uri = data.getData();
Uri fileUri = converUri(uri);
startImageZoom(fileUri);
} else if (requestCode == CROP_REQUEST_CODE) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
imageView.setImageBitmap(bitmap);
sendImage(bitmap);
}
}
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);
}
/**
* 将Content的Uri转成file的Uri
*
* @param uri
* @return
*/
private Uri converUri(Uri uri) {
InputStream in = null;
try {
in = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
return saveBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 将Bitmap转成file的Uri
*
* @param bitmap
* @return
*/
private Uri saveBitmap(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory() + "/aaa");
if (!file.exists())
file.mkdirs();
File imgFile = new File(file.getAbsolutePath() + "/1.jpeg");
if (imgFile.exists())
imgFile.delete();
try {
FileOutputStream outputStream = new FileOutputStream(imgFile);
bitmap.compress(CompressFormat.JPEG, 85, outputStream);
outputStream.flush();
outputStream.close();
Uri uri = Uri.fromFile(imgFile);
return uri;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 上传图片到服务器
*
* @param bitmap
*/
private void sendImage(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 80, outputStream);
byte[] bs = outputStream.toByteArray();
// 设置默认编码
String img = new String(Base64.encodeToString(bs, Base64.DEFAULT));
// 上传文件
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("img", img);
client.post("192.168.100.239/ImageUpload.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
// 上传成功
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
// 上传失败
}
});
}
}
图片上传使用了android-async-http-1.4.6 的第三方的包。
DEMO地址