Android: 单击按钮但方法未启动
Android: Button clicked but Method did not start
您好,我正在尝试创建一个小 Activity,它有一个 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 的 link。
现在我遇到了 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[=13=]0(MenuActivity.java:27)
at org.artoolkit.ar.samples.nftSimple.MenuActivity.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>
清单中的提供商:
<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>
尝试在您的 AndroidManifest 中添加以下内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
您的问题的另一种可能解决方案是:
我在 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()".
意味着它将 return "/data/org.artoolkit.ar.samples.nftSimple/cache/".
您只需要提供 "pictures/" 这样的子文件夹即可
"Android/data/org.artoolkit.ar.samples.nftSimple/cache/pictures/".
你的 file_paths.xml 应该喜欢这个
<cache-path name="my_images" path="target/" />
希望对你有所帮助
您好,我正在尝试创建一个小 Activity,它有一个 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 的 link。 现在我遇到了 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[=13=]0(MenuActivity.java:27)
at org.artoolkit.ar.samples.nftSimple.MenuActivity.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>
清单中的提供商:
<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>
尝试在您的 AndroidManifest 中添加以下内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
您的问题的另一种可能解决方案是:
我在 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()".
意味着它将 return "/data/org.artoolkit.ar.samples.nftSimple/cache/".
您只需要提供 "pictures/" 这样的子文件夹即可 "Android/data/org.artoolkit.ar.samples.nftSimple/cache/pictures/".
你的 file_paths.xml 应该喜欢这个
<cache-path name="my_images" path="target/" />
希望对你有所帮助