App crashes due to java.lang.SecurityException

前端 未结 5 919
甜味超标
甜味超标 2021-01-17 01:16

I am working on the project of capturing photos or picking images from gallery and show it in the recycler view, the app is working good in Android-lollipop but crashes in m

相关标签:
5条回答
  • 2021-01-17 01:38

    Just a thought, have you allowed your app to access your phone or SD card in the Manifest?

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    
    0 讨论(0)
  • 2021-01-17 01:45

    You need to give android.hardware.Camera permission programmatically.

    manifeast permission not work on Android Marshmallow

    In marshmallow,We require runtime permissions for Storage,Contacts,Camera, etc. In edition to give these permissions in manifest for older version, We need to request them from users at Runtime for marshmallow. For more details refer this : Marshmallow permissions

    0 讨论(0)
  • 2021-01-17 01:53

    You need to add

    <uses-permission android:name="android.permission.CAMERA" />
    
    0 讨论(0)
  • 2021-01-17 01:55

    you will need this permission too.

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

    and for writing data to storage you will need runtime permission
    make request

     ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_STORAGE);
    

    and check if user granted or decline to the permission request.

         @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_WRITE_STORAGE: {
    
                if (grantResults.length == 0
                        || grantResults[0] !=
                        PackageManager.PERMISSION_GRANTED) {
    
                    Log.i(TAG, "Permission has been denied by user");
    
                } else {
    
                    Log.i(TAG, "Permission has been granted by user");
    
                }
                return;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-17 01:58

    You must put Camera permission in code since android 6 + version checks for runtime permission.

    public void getCameraPermission(){
        if (!checkPermission()) {
            requestPermission();
        }
    }
    
    private boolean checkPermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
    
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    }
    
    private void requestPermission(){
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)){
    
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
        } else {
    
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Toast.makeText(MainActivity.this,"Permission granted",Toast.LENGTH_SHORT).show();
                    //store permission in shared pref
    
                }
    
                else {
                    Toast.makeText(MainActivity.this,"Permission denied",Toast.LENGTH_SHORT).show();
                    //store permission in shared pref
                }
                break;
        }
    }
    
    0 讨论(0)
提交回复
热议问题