-1

I bought a cell phone to use for testing out my android apps. The cell phone API level requires a minimum of 16. I have an app that I am creating that has some code that requres an API level of 23. When I tried to change the minumum level to 16, I got an error code that stated "call requires API level level 23 (Current min is 16) #checkselfpermission". This has to do with the bottom of my main activity, in which there is code to ask the user's permission for access to their images. Is there a way to change it so that I can try it on the cell phone? I am wondering about changing the Code, not the API.

 public class MainActivity extends AppCompatActivity {


        EditText editText2;


    public void getPhoto() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent,1);
    }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            if (requestCode ==1){
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);{
                getPhoto();
                }
            }
        }

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





                editText2 = (EditText) findViewById(R.id.editText2);

                editText2.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() ==
                                KeyEvent.ACTION_DOWN)) {
                            String currentStr = editText2.getText().toString();
                            String temp[] = currentStr.split("\n");
                            String result = "";
                            for (int i = 0; i < temp.length - 1; i++) {
                                result += temp[i] + "\n";
                            }
                            if (temp.length == 1) {
                                result = result + "\u2022 " + temp[temp.length - 1];
                            } else {
                                result = result.substring(0, result.length() - 1);
                                result = result + "\n\u2022 " + temp[temp.length - 1];
                            }
                            editText2.setText(result);
                            editText2.setSelection(editText2.getText().length());
                        }
                        return false;
                    }
                });


            }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);


            if (requestCode ==1 && resultCode == RESULT_OK && data != null) {
                Uri selectedImage = data.getData();

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);


                    ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
                    imageView2.setImageBitmap(bitmap);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

            public void click(View view) {
                if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                } else {
                    getPhoto();
            }
        }
    }
Fgf567
  • 21
  • 8

1 Answers1

1

storage Permission is require in API level 23 and above. For below API level 23, permission is automatically granted. Refer this page for more info: so you require to manage permission as per this:

if (Build.VERSION.SDK_INT < 23) {
        // your code 
} else {
    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            requestContactsPermissions1();
    } else {
        // your code
    }
}
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Nik
  • 1,991
  • 2
  • 13
  • 30