简体   繁体   中英

Android Runtime Permissions (Permission denied)

I had add uses-permission including WRITE_EXTERNAL_STORAGE, android.permission.CAMERA, READ_EXTERNAL_STORAGE to AndroidManifest.xml.

When I ran my app in Nexus6(API 24), it threw me the following error:

java.io.IOException: Permission denied

Here is my code:

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeProfileFragment extends Fragment {

    public static final int REQUEST_IMAGE_CAPTURE = 1;

    private String imageFilePath;

    public HomeProfileFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_profile, container, false);
        ///#################
        String[] perms = {"android.permission.RECORD_AUDIO", "android.permission.CAMERA"};

        File imageFile = null;
        try{
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                imageFile = createImageFile();
                imageFilePath = imageFile.getAbsolutePath();
            } else {
                /// Throw an error
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        int permsRequestCode = 200;

        requestPermissions(perms, permsRequestCode);
        ///#################


        if (imageFile != null){
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));

            if(takePictureIntent.resolveActivity(getActivity().getPackageManager())!=null){
                startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);

            }
        }
        return view;
    }

    ///##################
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

        switch(permsRequestCode){

            case 200:

                boolean audioAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED;

                boolean cameraAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;

                break;

        }

    }

    ///##################

    private File createImageFile()throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String fileName = "JPEG_" + timeStamp + "_";

        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        File image = File.createTempFile(fileName, ".jpg", storageDir);
        return image;

    }


    private void addPhotoToGallery(String filepath){
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(filepath);
        Uri uri =Uri.fromFile(f);
        mediaScanIntent.setData(uri);

        getActivity().sendBroadcast(mediaScanIntent);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Activity.RESULT_CANCELED){
        } if (requestCode == Activity.RESULT_CANCELED){
            addPhotoToGallery(imageFilePath);
        }
    }
    ///#################################
    private boolean shouldAskPermission(){

        return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1);

    }
}

How can I use the permission camera runtime before it opened?

You have to declare the user permission in the manifest

<uses-feature android:name="android.hardware.camera2" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

It has to be in the manifest, so the installer can ask the user to grant these permissions to the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM