Android拍照或从本地选择图片上传 您所在的位置:网站首页 原神电脑怎么拍照上传照片 Android拍照或从本地选择图片上传

Android拍照或从本地选择图片上传

2023-03-26 08:24| 来源: 网络整理| 查看: 265

弹出popueWindow选择上传方式

IMG_3546.JPG

弹出popueWindow的方法

private void showPopueWindow(){ View popView = View.inflate(this,R.layout.popupwindow_camera_need,null); Button bt_album = (Button) popView.findViewById(R.id.btn_pop_album); Button bt_camera = (Button) popView.findViewById(R.id.btn_pop_camera); Button bt_cancle = (Button) popView.findViewById(R.id.btn_pop_cancel); //获取屏幕宽高 int weight = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels*1/3; final PopupWindow popupWindow = new PopupWindow(popView,weight,height); popupWindow.setAnimationStyle(R.style.anim_popup_dir); popupWindow.setFocusable(true); //点击外部popueWindow消失 popupWindow.setOutsideTouchable(true); bt_album.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); popupWindow.dismiss(); } }); bt_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takeCamera(RESULT_CAMERA_IMAGE); popupWindow.dismiss(); } }); bt_cancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); //popupWindow消失屏幕变为不透明 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1.0f; getWindow().setAttributes(lp); } }); //popupWindow出现屏幕变为半透明 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.5f; getWindow().setAttributes(lp); popupWindow.showAtLocation(popView, Gravity.BOTTOM,0,50); }

popueWindow的布局文件

调起照相机的方法

private void takeCamera(int num) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; photoFile = createImageFile(); // Continue only if the File was successfully created if (photoFile != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); } } startActivityForResult(takePictureIntent, num);//跳转界面传回拍照所得数据 } private File createImageFile() { File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File image = null; try { image = File.createTempFile( generateFileName(), /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); } catch (IOException e) { e.printStackTrace(); } mCurrentPhotoPath = image.getAbsolutePath(); return image; } public static String generateFileName() { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; return imageFileName; }

接收Intent传递回来的消息

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK ) { if (requestCode == RESULT_LOAD_IMAGE && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); final String picturePath = cursor.getString(columnIndex); upload(picturePath); cursor.close(); }else if (requestCode == RESULT_CAMERA_IMAGE){ SimpleTarget target = new SimpleTarget() { @Override public void onResourceReady(Bitmap resource, GlideAnimation


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有