调用相册与相机获取--路径

来源:互联网 发布:单位网络监控怎么办 编辑:程序博客网 时间:2024/06/06 07:16

直接开说,先检查权限,然后启动弹框

  //检查权限问题    private void inspectPermission() {        // 如果SDK版本大于或等于23才需要检查权限,否则直接拨弹出图库        if (Build.VERSION.SDK_INT >= 23) {            // 检查权限是否允许            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)                    != PackageManager.PERMISSION_GRANTED) {                Log.e("TAG", "没有权限");                // 没有权限,考虑是否需要解释为什么需要这个权限                /*申请权限的解释,该方法在用户上一次已经拒绝过你的这个权限申请时使用。                * 也就是说,用户已经拒绝一次了,你又弹个授权框,你需要给用户一个解释,为什么要授权*/                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {                    Log.e("TAG", "没有权限,用户上次已经拒绝该权限,解释为什么需要这个权限");                    // Show an expanation to the user *asynchronously* -- don't block this thread                    //waiting for the user's response! After the user                    // sees the explanation, try again to request the permission.                    Toast.makeText(this, "需要权限才能上传图片哦", Toast.LENGTH_SHORT).show();                } else {                    Log.e("TAG", "没有权限,申请权限");                    // 申请权限                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CALL_PHONE);                }            } else {                Log.e("ydp", "有权限,执行相关操作");                // 有权限,执行相关操作                toPic();            }            //当是6.0以下版本时直接执行弹出拍照图库窗口        } else {            toPic();        }    }
 //去图库的方法    private void toPic() {        // 写一个去图库选图的Intent        Intent intent1 = new Intent(Intent.ACTION_PICK);        intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");        // 写一个打开系统拍照程序的intent        Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");        cameraPath = file.getAbsolutePath();        photoUri = Uri.fromFile(file);        intent2.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);        // Intent选择器        Intent intent = Intent.createChooser(intent1, "选择头像...");        intent.putExtra(Intent.EXTRA_INITIAL_INTENTS,                new Intent[]{intent2});        startActivityForResult(intent, 100);    }

这里是不同意权限的时候的回调

 @Override    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {        super.onRequestPermissionsResult(requestCode, permissions, grantResults);        if (requestCode == REQUEST_CALL_PHONE) {            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                Log.e(TAG, "权限同意");                toPic();            } else {                Log.e(TAG, "权限拒绝");                Toast.makeText(this, "权限拒绝", Toast.LENGTH_SHORT).show();            }            return;        }    }

接下来是回调了或去路径

 @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {   //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口                ContentResolver resolver = getContentResolver();//          //此处的用于判断接收的Activity是不是你想要的那个                // 作为头像的图片在设备上的本地路径是什么(/sdcard/XXX/XXXX.jpg)                String filePath = "";                if (data != null) {                    // 图片是用户从图库选择得到的                    // uri代表用户选取的图片在MediaStroe中存储的位置                    Uri uri = data.getData();                    // 利用ContentResolver查找uri所对应图片在设备上的真实路径                    Cursor cursor = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);                    cursor.moveToNext();                    filePath = cursor.getString(0);                    Log.i("hxl", "filePath========" + filePath);                    //显得到bitmap图片                    Bitmap bm = null;                    try {                        bm = MediaStore.Images.Media.getBitmap(resolver, uri);                    } catch (IOException e) {                        e.printStackTrace();                    }                    imgFileName = filePath.substring(filePath.lastIndexOf("/") + 1);                    Log.i("hxl", "imgFileName========" + imgFileName);                    File file = new File(filePath);                    Log.e("ydp", "img========================================" + filePath);                    Map<String, Object> hashMap = new HashMap<>();                    hashMap.put("seq", EMClient.getInstance().getCurrentUser());                    post_file("http://192.168.1.180:8082/rest/user/fileUpload", hashMap, file, bm);                    cursor.close();                } else {              ///  这是相机获取的路劲的代码              ///这的在源码有这里我就补贴出来了                }}

这是源码地址 Demo

0 0
原创粉丝点击