Android 拍照上传和拍照本地显示,拍照剪切上传,选取本地图片剪切上传
来源:互联网 发布:js是什么意思啊 编辑:程序博客网 时间:2024/04/30 14:45
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {switch (requestCode) {case TAKE_PICTURE://将保存在本地的图片取出并缩小后显示在界面上Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/image.jpg");Bitmap newBitmap = ImageTools.zoomBitmap(bitmap, bitmap.getWidth() / SCALE, bitmap.getHeight() / SCALE);//由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常bitmap.recycle();//将处理过的图片显示在界面上,并保存到本地iv_image.setImageBitmap(newBitmap);ImageTools.savePhotoToSDCard(newBitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), String.valueOf(System.currentTimeMillis()));break;case CHOOSE_PICTURE:ContentResolver resolver = getContentResolver();//照片的原始资源地址Uri originalUri = data.getData(); try { //使用ContentProvider通过URI获取原始图片Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri);if (photo != null) {//为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存Bitmap smallBitmap = ImageTools.zoomBitmap(photo, photo.getWidth() / SCALE, photo.getHeight() / SCALE);//释放原始图片占用的内存,防止out of memory异常发生photo.recycle();iv_image.setImageBitmap(smallBitmap);}} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) {e.printStackTrace();}break;case CROP:Uri uri = null;if (data != null) {uri = data.getData();System.out.println("Data");}else {System.out.println("File");String fileName = getSharedPreferences("temp",Context.MODE_WORLD_WRITEABLE).getString("tempName", "");uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),fileName));}cropImage(uri, 500, 500, CROP_PICTURE);break;case CROP_PICTURE:Bitmap photo = null;Uri photoUri = data.getData();if (photoUri != null) {photo = BitmapFactory.decodeFile(photoUri.getPath());}if (photo == null) {Bundle extra = data.getExtras();if (extra != null) { photo = (Bitmap)extra.get("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, stream); } }iv_image.setImageBitmap(photo);break;default:break;}}}public void showPicturePicker(Context context,boolean isCrop){final boolean crop = isCrop;AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("图片来源");builder.setNegativeButton("取消", null);builder.setItems(new String[]{"拍照","相册"}, new DialogInterface.OnClickListener() {//类型码int REQUEST_CODE;@Overridepublic void onClick(DialogInterface dialog, int which) {switch (which) {case TAKE_PICTURE:Uri imageUri = null;String fileName = null;Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);if (crop) {REQUEST_CODE = CROP;//删除上一次截图的临时文件SharedPreferences sharedPreferences = getSharedPreferences("temp",Context.MODE_WORLD_WRITEABLE);ImageTools.deletePhotoAtPathAndName(Environment.getExternalStorageDirectory().getAbsolutePath(), sharedPreferences.getString("tempName", ""));//保存本次截图临时文件名字fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";Editor editor = sharedPreferences.edit();editor.putString("tempName", fileName);editor.commit();}else {REQUEST_CODE = TAKE_PICTURE;fileName = "image.jpg";}imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),fileName));//指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(openCameraIntent, REQUEST_CODE);break;case CHOOSE_PICTURE:Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);if (crop) {REQUEST_CODE = CROP;}else {REQUEST_CODE = CHOOSE_PICTURE;}openAlbumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");startActivityForResult(openAlbumIntent, REQUEST_CODE);break;default:break;}}});builder.create().show();}//截取图片public void cropImage(Uri uri, int outputX, int outputY, int requestCode){Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", true); startActivityForResult(intent, requestCode);}
Demo下载
0 0
- Android 拍照上传和拍照本地显示,拍照剪切上传,选取本地图片剪切上传
- Android上传本地图片或者拍照上传
- Android 图片拍照上传、本地上传
- android 选择图片拍照并剪切照片上传到服务器
- Android拍照或本地图片上传
- Android设置拍照或者上传本地图片
- Android本地上传图片、拍照、裁剪
- android 从本地或者拍照上传图片
- Android获取本地图片或拍照上传
- HTML上传本地图片or拍照上传
- android 拍照上传及本地上传
- android:拍照/本地上传头像+本地缓存
- android 拍照剪切图片
- android 拍照 图片剪切
- 选择图片或拍照剪切后上传服务器
- 图片拍照上传和相册选取
- android拍照上传和本地上传功能实现了
- 本地-拍照上传到服务器
- Android SlidingMenu侧滑菜单使用介绍
- android:gravity和andrid:layout_gravity
- foreach循环
- Datatables.Net服务器端分页,客户与服务器端的交互过程数据
- JS与ANDROID通信
- Android 拍照上传和拍照本地显示,拍照剪切上传,选取本地图片剪切上传
- 应用云化 如何选择合适的云服务提供商
- Shell 加密之 shc 漏洞
- Android 自定义View (二)
- opencv中的一些陷阱
- Mac 配置Android Studio 出现 can't find valid JVM问题
- linux下Makefile学习之五(使用条件判断)
- smarty模板引擎(一)基础知识
- Android:获取手机屏幕宽高