Since I have migrated my app to Android N with FileProvider, I can't see my pictures in phone's gallery (even with version below Android N). So I created a small test app to debug this, without success.
In my Manifest
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<application
...>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
In file_paths.xml
<paths>
<external-files-path
name="test_images"
path="Pictures/TestPictures"/>
</paths>
And I made a small activity that shows last taken picture, to make sure that URI is correct :
private final static int REQUEST_CAMERA = 1102;
public static final String CAMERA_IMAGES_DIR = "TestPictures";
String mOutputFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onCamera();
}
});
}
public void onCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = createImageFile();
Uri photoURI = FileProvider.getUriForFile(this,
getPackageName() + ".fileprovider",
photoFile);
mOutputFilePath = photoFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CAMERA)
onCaptureImageResult();
}
private void onCaptureImageResult() {
if (mOutputFilePath != null) {
File f = new File(mOutputFilePath);
Uri finalUri = Uri.fromFile(f);
galleryAddPic(finalUri);
((ImageView) findViewById(R.id.imageView)).setImageURI(finalUri);
}
}
public File createImageFile() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), CAMERA_IMAGES_DIR + "/");
if (!storageDir.exists())
storageDir.mkdir();
return new File(storageDir, imageFileName + ".jpg");
}
private void galleryAddPic(Uri contentUri) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri);
sendBroadcast(mediaScanIntent);
}
My picture is in my app and in my file browser like expected but I can't see it in photo gallery. I tried to use FileProvider Uri, that's not works too.