问题
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);
}
来源:https://stackoverflow.com/questions/39321746/fileprovider-doesnt-work-for-image-capture