Android 拍照 录像 录音 选择本地图片 本地录像 本地录音的方法(自行调节)

/**
 * Created by wl on 2017/6/29.
 */

public class ZDLayout12 extends BaseView {

//    private static final String[] FJ = new String[]{"拍照", "录像", "录音", "本地图片", "本地录像", "本地录音"};
    private static final String[] FJ = new String[]{"拍照"};

//    public static final String dirFilePath = DirUtils.getInstance().getBigFileDir();

    private static final int TAKE_PHOTO = 1;
    private static final int TAKE_VIDEO = 3;
    private static final int TAKE_AUDIO = 4;
    private static final int IMG_FILE = 5;
    private static final int VIDEO_FILE = 6;
    private static final int AUDIO_FILE = 7;

    private String tempFilePath = null;
    private String tempDate = null;

    private ImageView ivAttach = null;
    private ImageView ivDelete;
    private int fileType;
    private String filePath;
    private String lastPath;

    private UploadResolver resolver = null;

    private boolean isEdit = false;

    public ZDLayout12(final ICallDialog icall, final BaseViewManager manager) {
        super(icall, manager);

        resolver = new UploadResolver(icall.getRLActivity());

        ivAttach = (ImageView) layout.findViewById(R.id.attachment);
        ivDelete = (ImageView) layout.findViewById(R.id.iv_fj_delete);

        try {
            File file = new File(dirFilePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {
            logger.e(e);
        }
        ivAttach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ivDelete.getVisibility() == View.VISIBLE) {
                    // TODO: 2017/7/7  预览
                    if (fileType == Attachment.FILETYPE_IMG) {
                        PreImageViewActivity.accessIn(icall.getRLActivity(), filePath);
                    } else if (fileType == Attachment.FILETYPE_VIDEO) {
                        videoShow(filePath);
                    } else if (fileType == Attachment.FILETYPE_AUDIO) {
                        videoShow(filePath);
                    }
                } else {
                    // TODO: 2017/7/7 添加 添加完成修改isEdit = true;
                    ((BaseActivity) icall.getRLActivity()).showRlDialog(10001, null);
                }
            }
        });

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fileType = 0;
                filePath = null;
                tempDate = null;
                ivDelete.setVisibility(View.GONE);
                ivAttach.setImageResource(R.drawable.add_attach);
                isEdit = true;
            }
        });

    }

    @Override
    public String getViewName() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.id.layout_12;
    }

    @Override
    public boolean isFinished() {
        isError = false;
        return true;
    }

    @Override
    public void getResult(JSONObject jsonObj) {
        JSONObject jsonObject = jsonObj.optJSONObject("personalAttachment");
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }
        try {
            if (isEdit) {
                jsonObject.put("fileType", fileType);
                jsonObject.put("filePath", filePath);

                if (StringUtils.isNull(filePath)) {
                    resolver.deleteAttachmentByAdd(manager.person.persionId);
                } else {
                    Attachment att = new Attachment();
                    att.cardNo = manager.person.cardNO;
                    att.personalId = manager.person.persionId;
                    att.personName = manager.person.name;
                    att.fileType = fileType;
                    att.filePath = filePath;
                    resolver.deleteAttachmentByAdd(att.personalId);
                    resolver.save(att);
                }
            }else if (!StringUtils.isNull(filePath)){
                Attachment att = new Attachment();
                att.cardNo = manager.person.cardNO;
                att.personalId = manager.person.persionId;
                att.personName = manager.person.name;
                att.fileType = fileType;
                att.filePath = filePath;
                resolver.deleteAttachmentByAdd(att.personalId);
                resolver.save(att);
            }
            jsonObj.put("personalAttachment", jsonObject);
        } catch (Exception e) {
            logger.e(e);
        }

    }

    @Override
    public void init(JSONObject jsonObj) {
        JSONObject jsonObject = jsonObj.optJSONObject("personalAttachment");
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }

        fileType = JsonUtils.getInt(jsonObject, "fileType");
        filePath = JsonUtils.getString(jsonObject, "filePath");
        lastPath = filePath;

        if (StringUtils.isNull(filePath)) {
        } else {
            ivDelete.setVisibility(View.VISIBLE);
            if (fileType == Attachment.FILETYPE_IMG) {
                if (filePath.startsWith("http") || filePath.startsWith("https")) {
                    IImage.displayImage(filePath, ivAttach);
                } else {
                    IImage.displayImage("file://" + filePath, ivAttach);
                }
            } else if (fileType == Attachment.FILETYPE_AUDIO) {
                ivAttach.setImageResource(R.drawable.fj_audio);
            } else if (fileType == Attachment.FILETYPE_VIDEO) {
                ivAttach.setImageResource(R.drawable.fj_video);
            }
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        File file = new File(tempFilePath);
                        if (file.exists()) {
                            filePath = tempFilePath;
                            fileType = Attachment.FILETYPE_IMG;
                            ivDelete.setVisibility(View.VISIBLE);
                            showImage();
                            isEdit = true;
                        }

                    } catch (Exception e) {
                        logger.e(e);
                    }
                }
                break;
            case TAKE_VIDEO:
                if (resultCode == RESULT_OK) {
                    try {
                        File file = new File(tempFilePath);
                        if (file.exists()) {
                            filePath = tempFilePath;
                            fileType = Attachment.FILETYPE_VIDEO;
                            ivDelete.setVisibility(View.VISIBLE);
                            ivAttach.setImageResource(R.drawable.fj_video);
                            isEdit = true;
                        }
                    } catch (Exception e) {
                        logger.e(e);
                    }
                }
                break;
            case TAKE_AUDIO:
                if (resultCode == RESULT_OK) {
                    try {
                        AssetFileDescriptor videoAsset = icall.getRLActivity().getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                        FileInputStream fis = videoAsset.createInputStream();
                        FileOutputStream fos = new FileOutputStream(tempFilePath);
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = fis.read(buf)) > 0) {
                            fos.write(buf, 0, len);
                        }
                        fis.close();
                        fos.close();
                        File file = new File(tempFilePath);
                        if (file.exists()) {
                            filePath = tempFilePath;
                            fileType = Attachment.FILETYPE_AUDIO;
                            ivDelete.setVisibility(View.VISIBLE);
                            ivAttach.setImageResource(R.drawable.fj_audio);
                            isEdit = true;
                        }
                    } catch (Exception e) {
                        logger.e(e);
                    }
                }
                break;
            case IMG_FILE:
                if (resultCode == RESULT_OK) {
                    try {
                        Uri uri = data.getData();
                        tempFilePath = FileUtils.getPath(icall.getRLActivity(), uri);
                        File file = new File(tempFilePath);
                        if (file.exists()) {
                            filePath = tempFilePath;
                            fileType = Attachment.FILETYPE_IMG;
                            ivDelete.setVisibility(View.VISIBLE);
                            showImage();
                            isEdit = true;
                        }
                    } catch (Exception e) {
                        logger.e(e);
                    }
                }
                break;
            case VIDEO_FILE:
                if (resultCode == RESULT_OK) {
                    try {
                        Uri uri = data.getData();
                        tempFilePath = FileUtils.getPath(icall.getRLActivity(), uri);
                        File file = new File(tempFilePath);
                        if (file.exists()) {
                            filePath = tempFilePath;
                            fileType = Attachment.FILETYPE_VIDEO;
                            ivDelete.setVisibility(View.VISIBLE);
                            ivAttach.setImageResource(R.drawable.fj_video);
                            isEdit = true;
                        }
                    } catch (Exception e) {
                        logger.e(e);
                    }
                }
                break;
            case AUDIO_FILE:
                if (resultCode == RESULT_OK) {
                    try {
                        Uri uri = data.getData();
                        tempFilePath = FileUtils.getPath(icall.getRLActivity(), uri);
                        File file = new File(tempFilePath);
                        if (file.exists()) {
                            filePath = tempFilePath;
                            fileType = Attachment.FILETYPE_AUDIO;
                            ivDelete.setVisibility(View.VISIBLE);
                            ivAttach.setImageResource(R.drawable.fj_audio);
                            isEdit = true;
                        }
                    } catch (Exception e) {
                        logger.e(e);
                    }
                }
                break;
            default:
                break;
        }
    }

    //调用系统相机
    private void cameraMethod() {
        tempDate = String.valueOf(System.currentTimeMillis());
        File outputImage = new File(dirFilePath, "image_" + tempDate + ".jpg");
        try {
            if (outputImage.exists()) {
                outputImage.delete();
            }
            outputImage.createNewFile();
        } catch (IOException e) {
            logger.e(e);
        }
        Uri uri = Uri.fromFile(outputImage);
        tempFilePath = outputImage.getAbsolutePath();
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        try {
            icall.getRLActivity().startActivityForResult(intent, TAKE_PHOTO);
        } catch (Exception e) {
            logger.e(e);
        }
    }

    //调用系统录像
    private void videoMethod() {
        tempDate = String.valueOf(System.currentTimeMillis());
        File outputVideo = new File(dirFilePath, "video_" + tempDate + ".mp4");
        try {
            if (outputVideo.exists()) {
                outputVideo.delete();
            }
            outputVideo.createNewFile();
        } catch (IOException e) {
            logger.e(e);
        }
        Uri uri = Uri.fromFile(outputVideo);
        tempFilePath = outputVideo.getAbsolutePath();
        Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        try {
            icall.getRLActivity().startActivityForResult(intent, TAKE_VIDEO);
        } catch (Exception e) {
            logger.e(e);
        }
    }

    //调用系统录音
    private void audioMethod() {
        File outputAudio = new File(dirFilePath, "audio_" + System.currentTimeMillis() + ".amr");
        try {
            if (outputAudio.exists()) {
                outputAudio.delete();
            }
            outputAudio.createNewFile();
        } catch (IOException e) {
            logger.e(e);
        }
        tempFilePath = outputAudio.getAbsolutePath();
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/amr");
        intent.setClassName("com.android.soundrecorder",
                "com.android.soundrecorder.SoundRecorder");
        try {
            icall.getRLActivity().startActivityForResult(intent, TAKE_AUDIO);
        } catch (Exception e) {
            logger.e(e);
        }
    }

    //播放录像
    private void videoShow(String filePath) {
        Uri uri = Uri.parse(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setDataAndType(uri, "video/*;audio/*");
        try {
            icall.getRLActivity().startActivity(intent);
        } catch (Exception e) {
            logger.e(e);
        }
    }

    private void imageFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");//图片
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            icall.getRLActivity().startActivityForResult(intent, IMG_FILE);
        } catch (android.content.ActivityNotFoundException ex) {
            RLToast.showRLToast(icall.getRLActivity(), "请安装一个文件管理器");
        }
    }

    private void videoFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("video/*");//选择视频
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            icall.getRLActivity().startActivityForResult(intent, VIDEO_FILE);
        } catch (android.content.ActivityNotFoundException ex) {
            RLToast.showRLToast(icall.getRLActivity(), "请安装一个文件管理器");
        }
    }

    private void audioFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/*");//选择音频
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            icall.getRLActivity().startActivityForResult(intent, AUDIO_FILE);
        } catch (android.content.ActivityNotFoundException ex) {
            RLToast.showRLToast(icall.getRLActivity(), "请安装一个文件管理器");
        }
    }

    private void showImage() {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                ((BaseActivity) icall.getRLActivity()).showLoading(false);
                IImage.displayImage("file://" + ImageCompress.thirdCompress(new File(filePath)).getAbsolutePath(), ivAttach);
                ((BaseActivity) icall.getRLActivity()).dismissLoading();
            }
        });
    }

    public Dialog onCreateDialog(int id, Bundle args) {
        if (id == 10001) {
            ActionSheetListDialog dialog = new ActionSheetListDialog(icall.getRLActivity());
            dialog.setOnItemClickListener(new ActionSheetListDialog.OnItemClickListener() {
                @Override
                public void onItemClickListener(String text, int which) {
                    if (which != -1) {
                        switch (text) {
                            case "拍照":
                                cameraMethod();
                                break;
                            case "录像":
                                videoMethod();
                                break;
                            case "录音":
                                audioMethod();
                                break;
                            case "本地图片":
                                imageFile();
                                break;
                            case "本地录像":
                                videoFile();
                                break;
                            case "本地录音":
                                audioFile();
                                break;
                            default:
                                break;
                        }
                    }
                }
            });
            try {
                JSONObject json = new JSONObject();
                JSONArray array = new JSONArray();
                for (String t : FJ) {
                    JSONObject j = new JSONObject();
                    j.put("title", t);
                    j.put("color", "#333333");
                    array.put(j);
                }
                json.put("item", array);
                JSONObject cancelJ = new JSONObject();
                cancelJ.put("title", "取消");
                cancelJ.put("color", "#ff0000");
                json.put("cancel", cancelJ);
                dialog.setData(json, "");
            } catch (Exception e) {
                logger.e(e);
            }

            return dialog;
        }
        return null;

    }

}

猜你喜欢

转载自blog.csdn.net/wl724120268/article/details/80507296