0

I am trying to use the cameraKit library to capture an image and save it to my gallery using an onClick method. The onClick method is not working and I can't figure out why, files are not saving to the gallery or the DCIM.

I have added permission for the app to write external storage but this has not made a difference.

package com.example.camerakitqr;



public class Scanner extends AppCompatActivity {
private CameraKitView cameraKitView;
private Button  detectButton;
//GraphicOverlay graphicOverlay;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cameraKitView = findViewById(R.id.camera);
    detectButton = findViewById(R.id.detectButton);
    detectButton.setOnClickListener(detectOnClickListener);

}

@Override
protected void onStart() {
    super.onStart();
    cameraKitView.onStart();

}

@Override
protected void onResume() {
    super.onResume();
    cameraKitView.onResume();
}

@Override
protected void onPause() {
    cameraKitView.onPause();
    super.onPause();
}

@Override
protected void onStop() {
    cameraKitView.onStop();
    super.onStop();
}

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








private View.OnClickListener detectOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         cameraKitView.captureImage(new CameraKitView.ImageCallback() {
             @Override
             public void onImage(CameraKitView cameraKitView, byte[] photo ) {

                  File savedPhoto = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
                  try{
                      FileOutputStream outputStream = new FileOutputStream(savedPhoto.getPath());
                      outputStream.write(photo);
                      outputStream.close();
                      Toast.makeText(getApplicationContext(), "File saved", Toast.LENGTH_SHORT).show();
                  } catch (java.io.IOException e){
                      e.printStackTrace();
                      Log.e("DEMO", "Not saved");
                  }


            }
         });


    }


};
  • https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Jun 20 '19 at 17:05
  • Note that your image is not going to be in a `DCIM/` directory, as that is not where you are writing it. Also note that you cannot write to arbitrary places on external storage on Android Q (by default) and Android R (for all apps, apparently). I recommend that you – CommonsWare Jun 20 '19 at 17:06
  • If you find my answer is helpful then please vote for it – Jasurbek Jul 01 '20 at 17:12

1 Answers1

0

Yes you are right you have added permissions at your manifest but if your device is Android 6+ then you should check runtime permission. In your code, I could not find this part. Official Site of Android Developers explains how to request runtime permissions at run time. Hope it helps.

You should ask permissions for using a camera as well as saving the photo (READ/WRITE to external storage)

Jasurbek
  • 2,946
  • 3
  • 20
  • 37