package com.example.helloworld;
import java.io.File;
import com.a.a.Tempest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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.provider.OpenableColumns;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class PhotoActivity extends Activity {
private static final String TAG="photoactivity";
private static final int CROP_SMALL_PICTURE = 0;
private static final int TAKE_PICTURE = 1;
private static final int CHOOSE_PICTURE = 2;
private ImageView iv_iamge;
private Bitmap mBitmap;
Uri tmpUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv_iamge=(ImageView) findViewById(R.id.iv_image);
}
public void upload(View v){
AlertDialog.Builder builder=new AlertDialog.Builder(PhotoActivity.this);
builder.setTitle("添加图片");
String [] items={"从相册选择照片","拍照"
};
builder.setNegativeButton("取消", null);
builder.setItems(items,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case CHOOSE_PICTURE:
Intent openIntent=new Intent(Intent.ACTION_GET_CONTENT);
openIntent.setType("image/*");
startActivityForResult(openIntent, CHOOSE_PICTURE);
break;
case TAKE_PICTURE:
Intent openCamreaIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
tmpUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"test_image.jpg"));
startActivityForResult(openCamreaIntent, TAKE_PICTURE);
break;
default:
break;
}
}
});
builder.show();
}
/**裁剪图片的方法
*
*/
protected void cutImage(Uri uri){
if(uri==null){
Log.i(TAG, "this is not exist");
}
tmpUri=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_SMALL_PICTURE);
}
/**
* 保存裁剪之后的图片并显示到界面上
*/
protected void setImageToView(Intent data){
Bundle extras=data.getExtras();
if(extras!=null){
mBitmap=extras.getParcelable("data");
iv_iamge.setImageBitmap(mBitmap);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
cutImage(tmpUri);
break;
case CHOOSE_PICTURE:
cutImage(data.getData());
break;
case CROP_SMALL_PICTURE:
if(data!=null){
setImageToView(data);
}
break;
default:
break;
}
}
}