android 拍照及从相册选择组件

单独封装到一个 activity 中便于更好的复用

拍照或从相册选择成功后使用 EventBus 发出广播回传图片路径,和调用者充分解耦合

根据传入参数支持裁剪和不裁剪两种模式

/** * <pre> * 拍照及从相册选择弹出 activity  * 成功后会发送 TakePhotoOutputEvent 事件,返回图片路径 * </pre> */public class TakePhotoPopupActivity extends Activity {    public static final int REQUEST_CODE_CAMERA = 110;    public static final int REQUEST_CODE_ALBUM = 111;    public static final int REQUEST_CODE_CROP = 112;    @ViewInject(R.id.takephoto_popup_layout)    private RelativeLayout outContainer;    @ViewInject(R.id.take_photo)    private TextView takePhoto;    @ViewInject(R.id.select_from_album)    private TextView openAlbum;    @ViewInject(R.id.cancel_photo)    private TextView cancel;    private String photoFileSavePath;    private String croppedFileSavePath;    private boolean isCrop;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_takephoto_popup);        ViewUtils.inject(this);        Intent intent = getIntent();        if (intent != null) {            photoFileSavePath = intent.getStringExtra("photoFileSavePath");            croppedFileSavePath = intent.getStringExtra("croppedFileSavePath");            isCrop = intent.getBooleanExtra("isCrop", false);        }    }    @OnClick({ R.id.take_photo, R.id.select_from_album, R.id.cancel_photo, R.id.takephoto_popup_layout })    public void buttonOnclick(View v) {        switch (v.getId()) {        case R.id.take_photo:            openCamera(photoFileSavePath);            break;        case R.id.select_from_album:            openAlbum();            break;        case R.id.cancel_photo:            finish();            break;        case R.id.takephoto_popup_layout:            finish();            break;        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        try {            if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) { // 相机                if (isCrop) {                    // 裁剪                    cropImage(photoFileSavePath, croppedFileSavePath);                } else {                    // 不裁剪                    EventBus.getDefault().post(new TakePhotoOutputEvent(requestCode, resultCode, photoFileSavePath));                    finish();                }            } else if (requestCode == REQUEST_CODE_ALBUM && resultCode == Activity.RESULT_OK) { // 相册                try {                    // 得到图片路径                    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]);                    String picPath = cursor.getString(columnIndex);                    cursor.close();                    if (isCrop) {                        cropImage(picPath, croppedFileSavePath);                    } else {                        EventBus.getDefault().post(new TakePhotoOutputEvent(requestCode, resultCode, picPath));                        finish();                    }                } catch (Exception e) {                }            } else if (requestCode == REQUEST_CODE_CROP && resultCode == Activity.RESULT_OK) { // 裁剪回来                EventBus.getDefault().post(new TakePhotoOutputEvent(requestCode, resultCode, croppedFileSavePath));                finish();            }        } catch (Exception e) {            e.printStackTrace();        }    }    public void openAlbum() {        Intent intent = new Intent(Intent.ACTION_PICK);        intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");        startActivityForResult(intent, REQUEST_CODE_ALBUM);    }    /**     * 裁剪相册照片     *      */    public void cropImage(String originFilePath, String croppedFilePath) {        File originFile = new File(originFilePath);        if (originFile == null || !originFile.exists()) {            return;        }        try {            if (croppedFilePath == null) {                croppedFilePath = getExternalFilesDir(null) + "/" + "tmpcropped.png";                this.croppedFileSavePath = croppedFilePath;            }            Uri originUri = Uri.fromFile(new File(originFilePath));            Uri croppedFileUri = Uri.fromFile(new File(croppedFilePath));            Intent intent = new Intent("com.android.camera.action.CROP");            intent.setDataAndType(originUri, "image/*");            intent.putExtra("crop", true);            intent.putExtra("aspectX", 1);            intent.putExtra("aspectY", 1);            intent.putExtra("outputX", 120);            intent.putExtra("outputY", 120);            intent.putExtra("return-data", true);            intent.putExtra("output", croppedFileUri);            startActivityForResult(intent, REQUEST_CODE_CROP);        } catch (Exception e) {            e.printStackTrace();        }    }    private void openCamera(String filePath) {        try {            if (filePath == null) {                filePath = getExternalFilesDir(null) + "/" + "tmpcamera.png";                this.photoFileSavePath = filePath;            }            Uri uri = Uri.fromFile(new File(filePath));            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);            startActivityForResult(intent, REQUEST_CODE_CAMERA);        } catch (Exception e) {            e.printStackTrace();        }    }}

demo github: https://github.com/lesliebeijing/AndroidPickPhotoComponent

更多相关文章

  1. [Android]res下面layout增加子文件夹
  2. android 下载图片及时显示
  3. Android相册及小小秒表震动(17)
  4. (Android)为什么我们不需要导入android support v4包?
  5. Android入门:查看服务器图片应用
  6. React Native文件读写操作(更新)
  7. 用android来实现图片的绘制以及旋转缩放案例分析
  8. Android(安卓)8.1 关机充电动画(二)Uboot模式
  9. Android开发Error(建议收藏下来以备不时之需)android.os.Transacti

随机推荐

  1. android Uri获取真实路径转换成File的方
  2. ListView.setOnItemClickListener无效问
  3. Google Android(安卓)Market电子市场/应
  4. Android清除本地数据缓存代码
  5. Android(安卓)Studio-Gradle项目中添加JN
  6. android嵌套滑动- Material Design
  7. android 二级菜单、双ListView 仿美团、
  8. GitHub 优秀的 Android(安卓)开源项目
  9. Android使用ListView使用
  10. [连载 1/15] Android(安卓)从入门到精通E