您好我正在尝试创建一个具有Button,两个TextView和一个ImageView的Activity。单击按钮时,活动应发送一个Intent来捕获照片并将照片保存在缓存中。
已解决当前错误可在编辑下找到:
问题是,当我按下按钮时,没有任何反应。添加一些我发现的日志后,问题出现在以下方法中:
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;
}
这是从开发者文档中保存图像的示例,但我在此处更改了代码:
File storageDir = new File(this.getCacheDir(), "export");
绕过FileProvider错误。
有人知道我的代码有什么问题吗?
编辑:有关上述错误的解决方案,请按照Rahul的链接进行操作。 现在我遇到了FileProvider的另一个问题。
已解决,请参阅Ajith Pandians解决方案
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)
使用文件提供程序的代码:
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>
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>
答案 0 :(得分:1)
尝试在AndroidManifest中添加以下内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
也可能解决您的问题:
答案 1 :(得分:1)
我在file_paths.xml文件中发现了问题。
通过文件
<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()".
表示它将返回&#34; /data/org.artoolkit.ar.samples.nftSimple/cache /"。
您只需要提供类似&#34;图片/&#34;的子文件夹。要得到 &#34; Android设备/数据/ org.artoolkit.ar.samples.nftSimple /高速缓存/图片/&#34;
你的file_paths.xml应该喜欢这个
<cache-path name="my_images" path="target/" />
我希望它会对你有所帮助。