
一个简单的拍照 DEMO ,拍完照,跳转到系统裁剪界面,裁剪保存完毕后,并显示。
求大神指点!!!(抓狂中...)
但是,我在 SD 卡目录下,发现该文件output_image.jpg 是 0 字节。
运行环境是 SDK : 23 、安卓 6.0
权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
变量: private Uri imageUri;
主要逻辑代码如下:
//跳转到拍照,逻辑代码如下: takePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent i = new Intent("android.media.action.IMAGE_CAPTURE"); i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(i, TAKE_PHOTO); } }); //回调函数,先是拍完照后,返回 TAKE_PHOTO 。 //然后再跳转到裁剪界面,返回 CROP_PHOTO ,并显示图片 switch (requestCode) { case TAKE_PHOTO: if (resultCode == RESULT_OK) { Intent i = new Intent("com.android.camera.action.CROP"); i.setDataAndType(imageUri, "image/*"); i.putExtra("scale", true); i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(i, CROP_PHOTO); } break; case CROP_PHOTO: if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); photo.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; } 1 l8mEQ331 2016 年 5 月 6 日 我在 5.1 和 6.01 的系统上都试了一下,没问题。 之前猜可能是 6.0 的权限问题,但是也就 outputImage.createNewFile()这部分是需要手动获取权限。拍照调用系统相机的话应该是不需要的。所以换个手机试试?(比如 MIUI 有自己的权限管理机制,需要在设置里手动打开之类的) |
2 syimo OP @l8mEQ331 感谢,问题已解决。参考这里 https://segmentfault.com/q/1010000005066402 。 对了,我能问下一般这个参数 i.putExtra("scale", true); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); 去哪里查找资料啊? |