源码:
MainActivity.java:
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int PHOTO_REQUEST_CAREMA = 1;//take photo
private static final int PHOTO_REQUEST_GALLERY = 2;//take from gallary
private static final int PHOTO_REQUEST_CUT = 3;//crop
private ImageView iv_image;
private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
private File tempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv_image = (ImageView) this.findViewById(R.id.iv_image);
}
public void gallery(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
public void camera(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(),
PHOTO_FILE_NAME);
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
}
private void crop(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", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST_GALLERY) {
if (data != null) {
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CAREMA) {
if (hasSdcard()) {
crop(Uri.fromFile(tempFile));
} else {
Toast.makeText(MainActivity.this, "scard wrong", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
this.iv_image.setImageBitmap(bitmap);
}
try {
tempFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
activity_main.xml:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="gallery"
android:text="获取图库图片" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="camera"
android:text="拍照获取图片" />
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>