Camera is not working in Nougat 7.0

后端 未结 4 1651
感动是毒
感动是毒 2021-02-15 12:25

My camera code is working in all Android versions but in Nougat 7.0 it gives the following error:

java.lang.NullPointerException: Attempt to invoke virtual meth         


        
4条回答
  •  眼角桃花
    2021-02-15 12:29

    Do this on click of camera button or camera image click event

    Uri fileUri;
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {
         ContentValues values = new ContentValues(1);
         values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
         fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
         cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
         Log.d("FILEURI",fileUri+"");
         editor.putString("Fileurl", fileUri+"");
         editor.commit();
         cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
         startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
    

    Do this in onActivityResult method

    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
    
        try {
             String utlstr =preferences.getString("Fileurl","");
             Log.d("sad",utlstr);
             Uri uri = Uri.parse(utlstr);
    
             final File file = inputStreamToFile(getContentResolver().openInputStream(uri), "png");
    
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         }        
     }
    

提交回复
热议问题