Android实现从手机相册上传头像以及拍照上传到服务器

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 {
    //2 1 0
    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;
    //private Button btn_upload;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        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) {
                // TODO Auto-generated method stub
                switch (which) {
                case CHOOSE_PICTURE:
                    Intent openIntent=new Intent(Intent.ACTION_GET_CONTENT);
                     openIntent.setType("image/*");
                       //用startActivityForResult方法,重写onActivityResult()方法,拿到图片进行裁剪操作
                    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"));
                                            // 将拍照所得的相片保存到SD卡根目录openCamreaIntent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
                    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);
        //设置aspectX y设置宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        //outputX 剪裁图片的宽高
        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) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
        // 对图片进行裁剪处理
            cutImage(tmpUri);
            break;
       case CHOOSE_PICTURE:
        // 对图片进行裁剪处理返回
           cutImage(data.getData());

            break;
       case CROP_SMALL_PICTURE:
            //将裁剪后的图片上传并设置到到ImageView控件中
           if(data!=null){
               setImageToView(data);
           }

    break;


        default:
            break;
        }
    }

}

你可能感兴趣的:(个人Android技术文章,Android基础篇)