Hello I am trying to create a little Activity which has a Button, two TextViews and a ImageView. The Activity should send a Intent for capturing a Photo and saving the Photo in cache, when the button has been clicked.
SOLVED current Error can be found under EDIT:
The Problem is, that when I hit the button, nothing happens. After adding some Logs I found out, that the Problem is in the following Method:
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(this.getCacheDir(), "target");
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
It is the example for saving a Image from the Developer Documentation but I changed the code here:
File storageDir = new File(this.getCacheDir(), "export");
To get around the FileProvider Error.
Does somebody know what is wrong with my code?
EDIT: For the Solution of the Error above follow the link from Rahul. Now I get another Problem with the FileProvider.
SOLVED see Ajith Pandians Answer for Solution
Logcat:
09-15 10:44:31.551 14882-14882/org.artoolkit.ar.samples.NftSimple E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.artoolkit.ar.samples.NftSimple, PID: 14882
java.lang.IllegalArgumentException:Failed to find configured root that contains /data/data/org.artoolkit.ar.samples.NftSimple/cache/targets/JPEG_20160915_104431_363552678.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
at org.artoolkit.ar.samples.nftSimple.MenuActivity.dispatchTakePictureIntent(MenuActivity.java:72)
at org.artoolkit.ar.samples.nftSimple.MenuActivity.access$000(MenuActivity.java:27)
at org.artoolkit.ar.samples.nftSimple.MenuActivity$1.onClick(MenuActivity.java:46)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
And the Code that uses the File Provider:
private void dispatchTakePictureIntent() {
Log.i(TAG, "I try to use my Method");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.i(TAG, "Error while creating the Image: " + ex.getMessage());
ex.printStackTrace();
}
if(photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"org.artoolkit.ar.samples.nftSimple.fileprovider",
photoFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
}
file_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="my_images" path="Android/data/org.artoolkit.ar.samples.nftSimple/files/Pictures" />
</paths>
Provider in Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="org.artoolkit.ar.samples.nftSimple.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
I found the problem on file_paths.xml file.
By the document
<cache-path name="name" path="path" />
"Represents files in the cache subdirectory of your app's internal storage area.
The root path of this subdirectory is the same as the value returned by getCacheDir()".
means it will return "/data/org.artoolkit.ar.samples.nftSimple/cache/".
You just need to give subfolder like "pictures/" to get "Android/data/org.artoolkit.ar.samples.nftSimple/cache/pictures/".
Your file_paths.xml should like this
<cache-path name="my_images" path="target/" />
I hope it will help you.