3

Code:-

  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                                   
        try{
               imageUri = Uri.fromFile(File.createTempFile("image", ".jpg"));
           }catch (Exception ex){
               ex.printStackTrace();
          }

   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, REQUEST_CAMERA);

I am using all permissions related to this in android manifest file... that above intent is working fine in mot g3 turbo and many more devices but in the case of only nexus 5 the resultCode is coming 0.. why?

Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
  • 2
    There is no requirement for `File.createTempFile()` to return a file path that is world-writeable. I would expect this code to fail on lots of devices. – CommonsWare Jun 15 '16 at 12:32
  • 2
    [This sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Camera/Content) uses files on external storage. However, that is going to start failing as a technique, as [Android N is beginning to ban `file:` `Uri` values](https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html). [This sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider) is the same as the first one, but uses `FileProvider`, to allow the third-party camera app to save the image directly to my app's internal storage. – CommonsWare Jun 15 '16 at 12:39
  • "still I used the Uri for fulfill my requirement" -- you can never raise your `targetSdkVersion` above 23. – CommonsWare Jun 16 '16 at 11:03
  • @CommonsWare...That's why i used this sir...actually my targetSDKVersion is 22... :) – Saurabh Vardani Jun 16 '16 at 11:18

1 Answers1

6

With the guidance of CommonsWare Sir, I resolved my problem by doing the following changes...

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
destination = Environment.getExternalStorageDirectory().getPath() + "/image.jpg";
outputUri= Uri.fromFile(new File(destination));
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
  intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
  List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  for (ResolveInfo resolveInfo : resInfoList) {
      String packageName = resolveInfo.activityInfo.packageName;
      grantUriPermission(packageName, outputUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
  }
}         

startActivityForResult(intent, REQUEST_CAMERA); 
Dika
  • 2,213
  • 4
  • 33
  • 49
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
  • 1
    @Hey Saurabh i am using your solution but still getting result code 0 in case of Nexus (6.0.1)but it's working in Moto Power E3 (version 6.0) – user2028 Jul 04 '17 at 11:10
  • got any solution for this, i am using nexus 6p , used this code, but still getting same problem – Noorul Sep 20 '18 at 05:12
  • 1
    Most likely it is because Environment#getExternalStorageDirectory is deprecated and I'm fairly certain it doesn't play well with devices that do not have an external SD card (or something similar). – Jensen Buck Mar 10 '23 at 19:05