• android 调用相机拍照及相册


    调用系统相机拍照:
    private Button btnDyxj;
    private ImageView img1;
    private File tempFile;
    btnDyxj = (Button) findViewById(R.id.btnXj);
    img1 = (ImageView) findViewById(R.id.img1);
    
    private void camera() {
        // 判断存储卡是否可以用,可用进行存储
        if (hasSdcard()) {
    
            File dir = new File(IMAGE_DIR);
            if (!dir.exists()) {
                dir.mkdir();
            }
            tempFile = new File(dir,
                    System.currentTimeMillis() + "_" + PHOTO_FILE_NAME);
            //从文件中创建uri
            Uri uri = Uri.fromFile(tempFile);
            Intent intent = new Intent();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.addCategory(intent.CATEGORY_DEFAULT);
            // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA
            startActivityForResult(intent, 1);
        } else {
            Toast.makeText(this, "未找到存储卡,无法拍照!", Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 判断sdcard是否被挂载
     */
    private boolean hasSdcard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                if (resultCode != RESULT_CANCELED) {
                    // 从相机返回的数据
                    if (hasSdcard()) {
                        if (tempFile != null) {
                            uploadImage(tempFile.getPath());
                        } else {
                            Toast.makeText(this, "相机异常请稍后再试!", Toast.LENGTH_SHORT).show();
                        }
    
                        Log.i("images", "拿到照片path=" + tempFile.getPath());
                    } else {
                        Toast.makeText(this, "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    
        }
    }
    
    /**
     * 上传图片
     *
     * @param path
     */
    private void uploadImage(final String path) {
        new Thread() {
            @Override
            public void run() {
                if (new File(path).exists()) {
                    Log.d("images", "源文件存在" + path);
                } else {
                    Log.d("images", "源文件不存在" + path);
                }
    
                File dir = new File(IMAGE_DIR);
                if (!dir.exists()) {
                    dir.mkdir();
                }
                final File file = new File(dir + "/temp_photo" + System.currentTimeMillis() + ".jpg");
                NativeUtil.compressBitmap(path, file.getAbsolutePath(), 50);
                if (file.exists()) {
                    Log.d("images", "压缩后的文件存在" + file.getAbsolutePath());
                } else {
                    Log.d("images", "压缩后的不存在" + file.getAbsolutePath());
                }
                Message message = new Message();
                message.what = 0xAAAAAAAA;
                message.obj = file.getAbsolutePath();
                handler.sendMessage(message);
    
            }
        }.start();
    
    }
    
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0xAAAAAAAA) {
                photoPath(msg.obj.toString());
            }
    
        }
    };
    
    public void photoPath(String path) {
        Glide.with(this)
                .load(path)
                .priority(Priority.HIGH)
                .into(img1);
    }

    调用系统化相册:

        /**
         * 从相册获取2
         */
        public void gallery() {
            Intent intent = new Intent(
                    Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, 2);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == 2) {
                    // 从相册返回的数据
                    if (data != null) {
                        // 得到图片的全路径
                        Uri uri = data.getData();
                        String[] proj = {MediaStore.Images.Media.DATA};
                        //好像是android多媒体数据库的封装接口,具体的看Android文档
                        Cursor cursor = managedQuery(uri, proj, null, null, null);
                        //按我个人理解 这个是获得用户选择的图片的索引值
                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        //将光标移至开头 ,这个很重要,不小心很容易引起越界
                        cursor.moveToFirst();
                        //最后根据索引值获取图片路径
                        String path = cursor.getString(column_index);
    
                        uploadImage(path);
                    }
                }
            }
        }
    
        /**
         * 上传图片
         *
         * @param path
         */
        private void uploadImage(final String path) {
            new Thread() {
                @Override
                public void run() {
                    if (new File(path).exists()) {
                        Log.d("images", "源文件存在" + path);
                    } else {
                        Log.d("images", "源文件不存在" + path);
                    }
    
                    File dir = new File(IMAGE_DIR);
                    if (!dir.exists()) {
                        dir.mkdir();
                    }
                    final File file = new File(dir + "/temp_photo" + System.currentTimeMillis() + ".jpg");
                    NativeUtil.compressBitmap(path, file.getAbsolutePath(), 50);
                    if (file.exists()) {
                        Log.d("images", "压缩后的文件存在" + file.getAbsolutePath());
                    } else {
                        Log.d("images", "压缩后的不存在" + file.getAbsolutePath());
                    }
                    Message message = new Message();
                    message.what = 0xAAAAAAAA;
                    message.obj = file.getAbsolutePath();
                    handler.sendMessage(message);
    
                }
            }.start();
        }
    
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0xAAAAAAAA) {
                    photoPath(msg.obj.toString());
                }
            }
        };
    
        public void photoPath(String path) {
            Map<String, Object> map = new HashMap<>();
            map.put("path", path);
            Glide.with(this)
                    .load(path)
                    .priority(Priority.HIGH)
                    .into(img1);
        }
  • 相关阅读:
    title()、upper()、lower()的用法
    Oracle用户无法访问ASM磁盘组问题
    Oracle-RAC监听服务进程关闭失败问题分析处理
    SHELL-字符串操作和变量替换
    Linux-按文件大小排序
    MySQL-重置root密码问题
    Oracle-表空间添加数据文件扩容出错解决措施
    Logstash-使用
    Logstash-工作原理
    ELK+filebeat+kafka搭建Oracle数据库日志平台
  • 原文地址:https://www.cnblogs.com/chhom/p/4760266.html
Copyright © 2020-2023  润新知