`

通过拍照或选择本地图片并剪裁图片设置成头像

阅读更多
还是直接上代码
import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class CropActivity extends BaseActivity {
	private Context context;
	private Button btn_crop;
	private String[] items = new String[] { "选择本地图片", "拍照" };
	
	/*头像名称*/ 
    private static final String IMAGE_FILE_NAME = "faceImage.jpg"; 
    
	/* 请求码*/ 
    private static final int IMAGE_REQUEST_CODE = 0;  
    private static final int CAMERA_REQUEST_CODE = 1;  
    private static final int RESULT_REQUEST_CODE = 2;  
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        context=this;
        initViews();
    }

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initValues();
	}

	@Override
	protected void updateViews(Object o) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		
		btn_crop=(Button)findViewById(R.id.btn_crop);
		btn_crop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog();
			}
		});
		
	}

	@Override
	protected void initValues() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void initHandler() {
		// TODO Auto-generated method stub

	}
	
	private void showDialog() {  
        
        new AlertDialog.Builder(this)  
                .setTitle("设置头像")  
                .setItems(items, new DialogInterface.OnClickListener() {  
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) {  
                        switch (which) {  
                        case 0:  
                            Intent intentFromGallery = new Intent();  
                            intentFromGallery.setType("image/*"); // 设置文件类型  
                            intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(intentFromGallery,IMAGE_REQUEST_CODE); 
                            break;  
                        case 1:  
                            Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            // 判断存储卡是否可以用,可用进行存储  
                            if (hasSdcard()) {  
                                intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,
                                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME)));
                            }  
                            startActivityForResult(intentFromCapture,CAMERA_REQUEST_CODE);
                            break;  
                        }  
                    }  
                })  
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {  
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) {  
                        dialog.dismiss();  
                    }  
                }).show();  
 
    }  
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 结果码不等于取消时候
		if (resultCode != RESULT_CANCELED) {
			switch (requestCode) {
			case IMAGE_REQUEST_CODE:
				startPhotoZoom(data.getData());
				break;
			case CAMERA_REQUEST_CODE:
				if (hasSdcard()) {
					File tempFile = new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME);
					startPhotoZoom(Uri.fromFile(tempFile));
				} else {
					showToast("未找到存储卡,无法存储照片!");
				}

				break;
			case RESULT_REQUEST_CODE:
				if (data != null) {
					setImageToView(data);
				}
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	 
	    /**  
	     * 裁剪图片方法实现  
	     *   
	     * @param uri  
	     */  
	    public void startPhotoZoom(Uri uri) {  
	    	if(uri==null){
				Log.i("tag", "The uri is not exist.");
			}
	        Intent intent = new Intent("com.android.camera.action.CROP");  
	        intent.setDataAndType(uri, "image/*");  
	        // 设置裁剪  
	        intent.putExtra("crop", "true");  
	        // aspectX aspectY 是宽高的比例  
	        intent.putExtra("aspectX", 1);  
	        intent.putExtra("aspectY", 1);  
	        // outputX outputY 是裁剪图片宽高  
	        intent.putExtra("outputX", 320);  
	        intent.putExtra("outputY", 320);  
	        intent.putExtra("return-data", true);  
	        startActivityForResult(intent, 2);  
	    }  
	 
	    /**  
	     * 保存裁剪之后的图片数据  
	     *   
	     * @param picdata  
	     */  
	    private void setImageToView(Intent data) {  
	        Bundle extras = data.getExtras();  
	        if (extras != null) {  
	            Bitmap photo = extras.getParcelable("data");  
	            Drawable drawable = new BitmapDrawable(photo);  
	            btn_crop.setBackgroundDrawable(drawable);
	        }  
	    }  
	
	
}



Android 根据角度旋转图片
http://blog.csdn.net/eastman520/article/details/18734069
分享到:
评论
1 楼 ding2wife 2016-04-05  
楼主这段代码用在华为手机4.4剪裁图片不好用

相关推荐

Global site tag (gtag.js) - Google Analytics