Search code examples
camerafile-uriandroid-7.1-nougat

Android N FileUriExposedException


Guys My app uses the stock camera to take a picture I use the following code to take a picture

public void takepic(View view) {

        TextView schtitle = (TextView) findViewById(R.id.Sitename);
        String schname = schtitle.getText().toString();
            String[] tokens = schname.split(" ");
            String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());
            String imageFileName = tokens[0] + "-" + timeStamp + ".jpg";
            TextView myAwesomeTextView = (TextView)findViewById(R.id.filetext);

          //in your OnCreate() method
          myAwesomeTextView.setText(imageFileName);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    


                File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                String name = imageFileName;
                File file = new File(path, name );
                outputFileUri = Uri.fromFile(file);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(intent, TAKE_PICTURE); 


       } 

This works fine in all but Nougat

I understand as they did in marshmallow you now need permissions and cant use file URI anymore

I cant find any sample code on how to take a picture in nougat could someone please point me in the right direction as to how I modify my code to allow this to happen?

Any help appreciated

Mark


Solution

  • The problem is the file:// uri is not permited anymore.

    Instead of this you must use FileProvider.

    Take a look here: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en