Android本地上传图片、拍照、裁剪

 

话不多说直接上代码

Notification
public class account extends AppCompatActivity
{
    public ImageView user_img;
    private String fileName = "";
    private File tempFile;
    private Bitmap bitmap;
    private int crop = 300;// 裁剪大小
    private static final int OPEN_CAMERA_CODE = 10;
    private static final int OPEN_GALLERY_CODE = 11;
    private static final int CROP_PHOTO_CODE = 12;
    private View headView;
    private View view;

    getYuanBitmap mGetYuanBitmap=new getYuanBitmap ();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_account );

        user_img=(ImageView)findViewById(R.id.user_img);
        user_img.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(account.this);
                //    指定下拉列表的显示数据
                final String[] cities = {"拍照", "本地上传"};
                //    设置一个下拉的列表选择项
                builder.setItems(cities, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        switch (which){
                            case 0:
                                initFile();
                                openCamera ();
                                break;
                            case 1:
                                initFile ();
                                openGallery();
                                break;
                        }

                    }
                });
                builder.show();

            }
        } );


    }
    /**
     * 创建文件路径
     */
    public void initFile() {
        if(fileName.equals("")) {
            File file = new File ( "/sdcard/pintu/" );
            file.mkdir();
            fileName = file.getPath () + "user_head_photo.jpg";
            tempFile = new File(fileName);
        }
    }


    /**
     * 调用相机
     */
    public void openCamera() {
        Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机
        intent.putExtra("output", Uri.fromFile(tempFile));
        startActivityForResult(intent, OPEN_CAMERA_CODE);
    }

    /**
     * 打开相册
     */
    public void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册
        intent.setDataAndType( MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
        intent.putExtra("output", Uri.fromFile(tempFile));
        startActivityForResult(intent, OPEN_GALLERY_CODE);
    }

    /**
     * 裁剪方形图片
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("output", Uri.fromFile(tempFile));
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", crop);
        intent.putExtra("outputY", crop);
        startActivityForResult(intent, CROP_PHOTO_CODE);
    }

    /**
     *返回图片地址
     *
     * */
    public String getFileName(){
        if (fileName!=null)
            return fileName;
        else
            return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1)
            return;

        switch (requestCode) {
            case OPEN_CAMERA_CODE:
                cropPhoto( Uri.fromFile(tempFile));
                break;
            case OPEN_GALLERY_CODE:
                cropPhoto(data.getData());
                break;
            case CROP_PHOTO_CODE:
                try {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    //合成Bitmap
                    bitmap = BitmapFactory.decodeFile(fileName, options);

                    if (bitmap!=null)
                    {
                        //得到圆形头像   
                        // 这个方法类看我的另一篇文章  http://blog.csdn.net/guang_liang_/article/details/52734930
                        Bitmap bitmap_photo=mGetYuanBitmap.toRoundBitmap ( bitmap,0 );

                        user_img.setImageBitmap ( bitmap_photo );
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

}

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页