调用系统的图库和系统相机完成获取头像功能 包括popupWindow的使用

忙着做项目,废话不多说,至于注释和代码解释什么的以后再补


public class MyInfoActivity extends Activity {
public final static int PHOTO_ZOOM = 0;
public final static int TAKE_PHOTO = 1;
public final static int PHOTO_RESULT = 2;
public static final String IMAGE_UNSPECIFIED = “image/*”;
private ImageView img;
private String imageDir;
private String path;
private Uri uri;

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

}

private void initView() {
    path = "/sdcard/like_see.bmp";
    uri = Uri.parse(path);
    img = (ImageView) findViewById(R.id.menu_myinfo_image1);
    File logopath = new File(path);
    if (logopath.exists()) {
        img.setImageURI(uri);
    }
}

public void onclick(View v) {
    switch (v.getId()) {
        case R.id.menu_myinfo_back:
            this.finish();
            break;
        case R.id.menu_myinfo_layout2:
            showPopupWindow(v);
            break;

    }
}


‘private void showPopupWindow(View view) {
//一个自定义 布局,作为显示的内容
View contentView = LayoutInflater.from(this).inflate(R.layout.activity_myinfo_popupwindow, null);
//设置按钮的点击事件
Button bt1 = (Button) contentView.findViewById(R.id.myinfo_popup_button1);
Button bt2 = (Button) contentView.findViewById(R.id.myinfo_popup_button2);
//创建出PopupWindow
final PopupWindow popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(IMAGE_UNSPECIFIED);
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, PHOTO_ZOOM);

popupWindow.dismiss();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageDir = “test.jpg”;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageDir)));
startActivityForResult(intent, TAKE_PHOTO);

            popupWindow.dismiss();
        }
    });
    // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
    // 我觉得这里是API的一个bug

    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.book_page_bg2));
    //设置宽度
    popupWindow.setWidth(view.getWidth() / 4 * 3);
    //设置显示位置
    popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, -100);
    //可点击
    popupWindow.setTouchable(true);
    popupWindow.setFocusable(true);
    popupWindow.setOutsideTouchable(true);

    popupWindow.setTouchInterceptor(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                popupWindow.dismiss();
                return true;
            }
            //如果返回true,touch事件将被拦截
            //拦截后,popupWindow的OnTouchEvennt不被调用,这样点击外部区域无法dismiss
            return false;

        }
    });


    //将popup展示出来
    popupWindow.showAsDropDown(view);

}

// 图片缩放
public void photoZoom(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
    intent.putExtra("crop", "true");
    // aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // outputX outputY 是裁剪图片宽高
    intent.putExtra("outputX", 250);
    intent.putExtra("outputY", 250);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PHOTO_RESULT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PHOTO_ZOOM) {
            photoZoom(data.getData());
        }
        if (requestCode == TAKE_PHOTO) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            File picture = new File(Environment.getExternalStorageDirectory() + "/" + imageDir);

            photoZoom(Uri.fromFile(picture));
        }

        if (requestCode == PHOTO_RESULT) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                //此处是为了将切图进行保存在sdcard中(记住声明系统权限啊)
                try {
                    File picture = new File(path);
                    if (!picture.exists()) {
                        picture.createNewFile();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(picture));
                    photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
                    bos.flush();
                    bos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("wuwu,no file");
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println(e.getMessage());
                }
                img.setImageBitmap(photo);
                try {
                    stream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println(e.getMessage());
                }
            }
        }


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

布局文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值