当前位置:   article > 正文

Android照相机和相册动态权限_安卓 相册图片 动态权限

安卓 相册图片 动态权限

1.相机

@OnClick(R.id.to_camear_btn)
    public void onClick() {

//        checkSelfPermission 检测有没有 权限
//        PackageManager.PERMISSION_GRANTED 有权限
//        PackageManager.PERMISSION_DENIED  拒绝权限
        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
            //权限发生了改变 true  //  false 小米
            if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){



                new AlertDialog.Builder(this).setTitle("title")
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 请求授权
                                ActivityCompat.requestPermissions(PermissionActivity.this,new String[]{Manifest.permission.CAMERA},1);

                            }
                        }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();



            }else {
                ActivityCompat.requestPermissions(PermissionActivity.this,new String[]{Manifest.permission.CAMERA},1);

            }

        }else{

            camear();

        }




    }


    /**
     *
     * @param requestCode
     * @param permissions 请求的权限
     * @param grantResults 请求权限返回的结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode == 1){
            // camear 权限回调

            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

                // 表示用户授权
                Toast.makeText(this, " user Permission" , Toast.LENGTH_SHORT).show();

                camear();


            } else {

                //用户拒绝权限
                Toast.makeText(this, " no Permission" , Toast.LENGTH_SHORT).show();

            }



        }

    }

    public void camear(){
        try {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
  7. @Override
  8. public void onClick(View view) {
  9. onTakePhoto();
  10. }
  11. });
  12. }
  13. public void onTakePhoto() {
  14. if (Build.VERSION.SDK_INT>=23) {
  15. int request= ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
  16. if (request!= PackageManager.PERMISSION_GRANTED)//缺少权限,进行权限申请
  17. {
  18. ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},123);
  19. return;//
  20. }
  21. else
  22. {
  23. //权限同意,不需要处理,去掉用拍照的方法
  24. // Toast.makeText(this,"权限同意",Toast.LENGTH_SHORT).show();
  25. }
  26. }
  27. else{
  28. //低于23 不需要特殊处理,去掉用拍照的方法
  29. }
  30. }
  31. //参数 requestCode是我们在申请权限的时候使用的唯一的申请码
  32. //String[] permission则是权限列表,一般用不到
  33. //int[] grantResults 是用户的操作响应,包含这权限是够请求成功
  34. //由于在权限申请的时候,我们就申请了一个权限,所以此处的数组的长度都是1
  35. @Override
  36. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  37. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  38. if (requestCode==123)
  39. { //当然权限多了,建议使用Switch,不必纠结于此
  40. if (grantResults[0]==PackageManager.PERMISSION_GRANTED) {
  41. Toast.makeText(this, "权限申请成功",Toast.LENGTH_SHORT).show();
  42. }else if (grantResults[0]== PackageManager.PERMISSION_DENIED) {
  43. Toast.makeText(this, "权限申请失败,用户拒绝权限", Toast.LENGTH_SHORT).show();
  44. }
  45. }
  46. }
  47. }

2.相册

  1. public class MainActivity extends AppCompatActivity {
  2. //调用系统相册-选择图片
  3. private static final int IMAGE = 1;
  4. //所需权限
  5. // <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. }
  11. public void onClick(View v) {
  12. //调用相册
  13. Intent intent = new Intent(Intent.ACTION_PICK,
  14. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  15. startActivityForResult(intent, IMAGE);
  16. }
  17. @Override
  18. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  19. super.onActivityResult(requestCode, resultCode, data);
  20. //获取图片路径
  21. if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
  22. Uri selectedImage = data.getData();
  23. String[] filePathColumns = {MediaStore.Images.Media.DATA};
  24. Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
  25. c.moveToFirst();
  26. int columnIndex = c.getColumnIndex(filePathColumns[0]);
  27. String imagePath = c.getString(columnIndex);
  28. showImage(imagePath);
  29. c.close();
  30. }
  31. }
  32. //加载图片
  33. private void showImage(String imaePath){
  34. Bitmap bm = BitmapFactory.decodeFile(imaePath);
  35. ((ImageView)findViewById(R.id.image)).setImageBitmap(bm);
  36. }
  37. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/902600
推荐阅读
相关标签
  

闽ICP备14008679号