Camera permission cache call

1. Button event

AddCarProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(AddCarProduct.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    //Apply for permission, REQUEST_TAKE_PHOTO_PERMISSION is a custom constant
                    ActivityCompat.requestPermissions(AddCarProduct.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
                }else {
                    //Have permission to take pictures directly
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// Start the system camera
                    startActivityForResult(intent,1);
                }
            }
        });

2. Get permission and rewrite directly

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 2) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               //Get permission successfully, you can take pictures
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// Start the system camera
                startActivityForResult(intent,1);
            } else {
                Toast.makeText(this, "Please enable "Camera" access in App Manager!", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }


3. The camera takes pictures and gets the return information

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK&&requestCode == 1) { // if return data
            Bitmap data1 = (Bitmap) data.getExtras().get("data");
            String base64 = PhotoUtils.bitmapToBase64(data1);
            map.clear();
            if (NetUtil.isNetAvailable(this)) {
                map.put("imgBase64",base64);
                Log.d("aaaaa", "onActivityResult: ");
                OKhttptils.post(this, Config.PARSEVIN, map, new OKhttptils.HttpCallBack() {
                    @Override
                    public void success(String response) {
                        Gson gson = GsonFactory.create ();
                        ChassisNumber chassisNumber = gson.fromJson(response, ChassisNumber.class);
                        String vin = chassisNumber.getData().getResult().getVin();
                        carCjh.setText(vin);
                    }

                    @Override
                    public void fail(String response) {
                        Toast.makeText(AddCarProduct.this, "Failed to get the frame number", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            // do cache storage
            if (resultCode == Activity.RESULT_OK) {
                String sdStatus = Environment.getExternalStorageState();
                if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // Check if sd is available
                    Log.i("TestFile", "SD card is not avaiable/writeable right now.");
                    return;
                }
                new DateFormat();
                String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
                Toast.makeText(this, name, Toast.LENGTH_LONG).show();
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");// Get the data returned by the camera and convert it to Bitmap image format

                FileOutputStream b = null;
                File file = new File("/sdcard/Image26/");
                file.mkdirs();//Create a folder
                String fileName = "/sdcard/Image/"+name;

                try {
                    b = new FileOutputStream(fileName);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// Write data to file
                } catch (FileNotFoundException e) {
                    e.printStackTrace ();
                } finally {
                    try {
                        b.flush();
                        b.close();
                    } catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
                try
                {

                }catch(Exception e)
                {
                    Log.e("error", e.getMessage());
                }

            }



        }
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889772&siteId=291194637