44
44
import android .net .Uri ;
45
45
46
46
import android .os .Bundle ;
47
- import android .os .Environment ;
48
47
import android .os .StrictMode ;
49
48
import android .preference .PreferenceManager ;
50
49
import android .provider .MediaStore ;
53
52
import androidx .annotation .NonNull ;
54
53
import androidx .core .app .ActivityCompat ;
55
54
import androidx .core .content .ContextCompat ;
55
+ import androidx .core .content .FileProvider ;
56
56
import android .util .Log ;
57
57
import android .view .KeyEvent ;
58
58
import android .view .Menu ;
@@ -75,7 +75,6 @@ public class TrackLogger extends Activity {
75
75
private static final String TAG = TrackLogger .class .getSimpleName ();
76
76
77
77
final private int RC_STORAGE_AUDIO_PERMISSIONS = 1 ;
78
- final private int RC_STORAGE_CAMERA_PERMISSIONS = 2 ;
79
78
80
79
/**
81
80
* Request code for callback after the camera application had taken a
@@ -526,34 +525,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
526
525
case KeyEvent .KEYCODE_CAMERA :
527
526
Log .d (TAG , "click on camera button" );
528
527
if (gpsLogger .isTracking ()) {
529
- if (ContextCompat .checkSelfPermission (this ,
530
- Manifest .permission .CAMERA ) != PackageManager .PERMISSION_GRANTED ) {
531
- Log .d (TAG , "camera permission isn't granted, will request" );
532
- // Should we show an explanation?
533
- if ( (ActivityCompat .shouldShowRequestPermissionRationale (this ,
534
- Manifest .permission .CAMERA )) ) {
535
-
536
- // Show an expanation to the user *asynchronously* -- don't block
537
- // this thread waiting for the user's response! After the user
538
- // sees the explanation, try again to request the permission.
539
- // TODO: explain why we need permission.
540
- Log .w (TAG , "we should explain why we need write and record audio permission" );
541
-
542
- } else {
543
-
544
- // No explanation needed, we can request the permission.
545
- ActivityCompat .requestPermissions (this ,
546
- new String []{
547
- Manifest .permission .CAMERA },
548
- RC_STORAGE_CAMERA_PERMISSIONS );
549
- break ;
550
- }
551
-
552
- } else {
553
- requestStillImage ();
554
- //return true;
555
- }
556
-
528
+ requestStillImage ();
557
529
}
558
530
break ;
559
531
case KeyEvent .KEYCODE_DPAD_CENTER :
@@ -800,8 +772,13 @@ public long getCurrentTrackId() {
800
772
private void startCamera (File imageFile ) {
801
773
StrictMode .VmPolicy .Builder builder = new StrictMode .VmPolicy .Builder ();
802
774
StrictMode .setVmPolicy (builder .build ());
775
+
776
+ Uri imageUriContent = FileProvider .getUriForFile (this ,
777
+ DataHelper .FILE_PROVIDER_AUTHORITY , imageFile );
778
+
803
779
Intent cameraIntent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
804
- cameraIntent .putExtra (MediaStore .EXTRA_OUTPUT , Uri .fromFile (imageFile ));
780
+ cameraIntent .addFlags (Intent .FLAG_GRANT_WRITE_URI_PERMISSION | Intent .FLAG_GRANT_READ_URI_PERMISSION );
781
+ cameraIntent .putExtra (MediaStore .EXTRA_OUTPUT , imageUriContent );
805
782
startActivityForResult (cameraIntent , REQCODE_IMAGE_CAPTURE );
806
783
}
807
784
@@ -837,27 +814,6 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
837
814
}
838
815
return ;
839
816
}
840
-
841
- case RC_STORAGE_CAMERA_PERMISSIONS : {
842
- Log .d (TAG , "camera case" );
843
- // If request is cancelled, the result arrays are empty.
844
- if (grantResults .length > 1 ) {
845
- // TODO: fix permission management
846
- //&& grantResults[0] == PackageManager.PERMISSION_GRANTED
847
- //&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
848
-
849
- // permission was granted, yay!
850
- requestStillImage ();
851
-
852
- } else {
853
-
854
- // permission denied, boo! Disable the
855
- // functionality that depends on this permission.
856
- //TODO: add an informative message.
857
- Log .v (TAG , "Camera permission is denied." );
858
- }
859
- return ;
860
- }
861
817
}
862
818
}
863
819
0 commit comments