4

i've spent a lot of time trying to find a solution. I tried getExternalStoragePublicDirectory() and getExternalFilesDir() and many other things. But onActivityResult still brings error resultCode. It works when I switch from fileProvider to Uri.fromFile() only. But Android manual strictly recommends to use FileProvider for new APIs. Please tell me what I'm doing wrong.

Manifest fragment:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.myapp.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
</provider>

file-paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path name="my_images" path="Android/data/com.mydomain.myapp/files/Pictures" />
 <external-path name="pic" path="Pictures" /> </paths>

Java

File photoFile;
public void buttonContentChanger(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        try{
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            if (!storageDir.exists()) storageDir.mkdir();
            photoFile = File.createTempFile(imageFileName, ".jpg", storageDir);
        } catch (Exception e) {}
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this, "com.mydomain.myapp.fileprovider", photoFile);
      //          Uri photoURI1 = Uri.fromFile(photoFile); Working!!!!
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, 1);
            }
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bitmap bb = BitmapFactory.decodeFile("file:" + photoFile.getAbsolutePath(), null);
}
Jaco
  • 923
  • 2
  • 14
  • 28
Alex
  • 41
  • 1
  • 2
  • 1
    Replace `` with ``. Your other `external-path` may not be reliable. Also, you need to add `FLAG_GRANT_WRITE_URI_PERMISSION` to the `Intent`. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider) for an example of using `FileProvider` with `ACTION_IMAGE_CAPTURE`. And bear in mind that your problem may be with the camera app itself not supporting `content` `Uri` values. Check LogCat for error messages that the camera app may have logged. – CommonsWare Sep 04 '16 at 21:50
  • 1
    Thanks a lot. Permisions really were the problem. I wonder why there is no about it here https://developer.android.com/training/camera/photobasics.html since android:grantUriPermissions="true" in the manifest is not enough. – Alex Sep 05 '16 at 03:05
  • 1
    By the way, replacing "external-path" with "external-files-path" caused exception in FileProvider.getUriForFile so i left "external-path" – Alex Sep 05 '16 at 03:12
  • Make sure that you are on the latest version of the Android Support libraries (presently 24.2.0). – CommonsWare Sep 05 '16 at 10:54

0 Answers0