Android-允许使用相机并写入外部存储

时间:2018-12-18 16:19:20

标签: java android android-permissions

我正在尝试使用相机并写入外部存储的权限。 我的主要活动是:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);





        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
                {
                    if(checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED)
                    {
                        ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.CAMERA});
                    }
                }

                if(ActivityCompat.checkSelfPermission(getApplicationContext(),
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
                {
                    ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_PERM_WRITE_STORAGE);

            }
                else
                {
                    takePhoto(); // calls this method
                }
            }
        });
    }

我已将其包含在Manifest.xml中

<uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我收到此错误:

 cannot find symbol method requestPermissions(MainActivity,String[])
cannot find symbol method checkSelfPermission(Context,String)

我做错了什么。谢谢

1 个答案:

答案 0 :(得分:0)

对于请求相机部分,请尝试

final int RequestCameraPermissionID = 1001;

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA},RequestCameraPermissionID);