@Override protected 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; @Override public 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); }
详细解决方案
Android 拍照上传和拍照本地显示,拍照剪切上传,选取本map片剪切上传
热度:36 发布时间:2016-04-28 03:14:12.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- android 教程解决方法
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java)中的java.net能不能和c#的system.net.sockets进行tcp通信,该如何解决
- android ListView 中的onItemClick Intent 没法跳转
- android(java) 中文乱码的有关问题
- c#c++,android,ios(iphone),php,java视屏课程 散分
- android Post文件到ASP.NET的有关问题,能收到参数收不到文件
- RIM 替 Android 开发者提供免费的 PlayBook!2月13日前
- android 动态设立控件高度
- Android test project 编译方法
- android -相机使用教程(1)解决方法