2023年7月15日发(作者:)
Android拍照或从本地选择图⽚上传弹出popueWindow选择上传⽅式IMG_弹出popueWindow的⽅法private void showPopueWindow(){ View popView = e(this,indow_camera_need,null); Button bt_album = (Button) ewById(_pop_album); Button bt_camera = (Button) ewById(_pop_camera); Button bt_cancle = (Button) ewById(_pop_cancel); //获取屏幕宽⾼ int weight = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels*1/3; final PopupWindow popupWindow = new PopupWindow(popView,weight,height); mationStyle(_popup_dir); usable(true); //点击外部popueWindow消失 sideTouchable(true); bt_lickListener(new kListener() { @Override public void onClick(View v) { Intent i = new Intent(_PICK, AL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); s(); } }); bt_lickListener(new kListener() { @Override public void onClick(View v) { takeCamera(RESULT_CAMERA_IMAGE); s(); } }); bt_lickListener(new kListener() { @Override public void onClick(View v) { s(); } }); //popupWindow消失屏幕变为不透明 ismissListener(new issListener() { @Override public void onDismiss() { Params lp = getWindow().getAttributes(); = 1.0f; getWindow().setAttributes(lp); } }); //popupWindow出现屏幕变为半透明 Params lp = getWindow().getAttributes(); = 0.5f; getWindow().setAttributes(lp); Location(popView, ,0,50); }popueWindow的布局⽂件 调起照相机的⽅法private void takeCamera(int num) { Intent takePictureIntent = new Intent(_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (eActivity(kageManager()) != 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) { ra(_OUTPUT, le(photoFile)); } } startActivityForResult(takePictureIntent, num);//跳转界⾯传回拍照所得数据 }private File createImageFile() { File storageDir = ernalStoragePublicDirectory(ORY_DCIM); File image = null; try { image = TempFile( generateFileName(), /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); } catch (IOException e) { tackTrace(); } mCurrentPhotoPath = olutePath(); 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) { vityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK ) { if (requestCode == RESULT_LOAD_IMAGE && null != data) { Uri selectedImage = a(); String[] filePathColumn = {}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); First(); int columnIndex = umnIndex(filePathColumn[0]); final String picturePath = ing(columnIndex); upload(picturePath); (); }else if (requestCode == RESULT_CAMERA_IMAGE){ SimpleTarget target = new SimpleTarget() { @Override public void onResourceReady(Bitmap resource, GlideAnimation super Bitmap> glideAnimation) { upload(saveMyBitmap(resource).getAbsolutePath()); } @Override public void onLoadStarted(Drawable placeholder) { public void onLoadStarted(Drawable placeholder) { Started(placeholder); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { Failed(e, errorDrawable); } }; ().load(mCurrentPhotoPath) .asBitmap() .diskCacheStrategy() .override(1080, 1920)//图⽚压缩 .centerCrop() .dontAnimate() .into(target); } } }//将bitmap转化为png格式 public File saveMyBitmap(Bitmap mBitmap){ File storageDir = ernalStoragePublicDirectory(ORY_DCIM); File file = null; try { file = TempFile( teFileName(), /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); FileOutputStream out=new FileOutputStream(file); ss(, 20, out); (); (); } catch (Exception e) { tackTrace(); } return file; }图⽚上传⽅法 private void upload(String picturePath) { final ProgressDialog pb= new ProgressDialog(this); sage("正在上传"); celable(false); (); imageUpLoad(picturePath, new Response() { @Override public void onSuccess(FileUpload response) { ess(response); if (s) { myFileId = ; runOnUiThread(new Runnable() { @Override public void run() { ortToast("上传成功"); s(); } }); } } @Override public void onFaile(String e) { e(e); runOnUiThread(new Runnable() { @Override public void run() { s(); ortToast("上传失败"); } }); } }); } public static void imageUpLoad(String localPath, final Response callBack) { MediaType MEDIA_TYPE_PNG = ("image/png"); OkHttpClient client = new OkHttpClient(); r builder = new r().setType(); File f = new File(localPath); mDataPart("file", e(), (MEDIA_TYPE_PNG, f)); final MultipartBody requestBody = (); //构建请求 final Request request = new r() .url(" ")//地址 .post(requestBody)//添加请求体 .build(); l(request).enqueue(new ck() { @Override public void onFailure(Call call, IOException e) { e(sage()); n("上传失败:alizedMessage() = " + alizedMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { FileUpload resultBean = new Gson().fromJson(().string(), ); ess(resultBean); } }); }
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689429919a246897.html
评论列表(0条)