Android 使用相机拍摄后保存全尺寸照片会显示onActivityResult结果代码0
我尝试在使用相机拍摄后保存全尺寸照片,但在OnActivity上,结果显示结果代码为0。我正在使用文件提供程序从filepath.xml中的文件获取Uri。下面您可以看到我的代码: 在filepath.xml中:Android 使用相机拍摄后保存全尺寸照片会显示onActivityResult结果代码0,android,android-camera,android-fileprovider,Android,Android Camera,Android Fileprovider,我尝试在使用相机拍摄后保存全尺寸照片,但在OnActivity上,结果显示结果代码为0。我正在使用文件提供程序从filepath.xml中的文件获取Uri。下面您可以看到我的代码: 在filepath.xml中: <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="my_images" path="my_images/"/> </pa
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="my_images/"/>
</paths>
在onActivityResult中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView imageView=null;
Bitmap bm=null;
try {
if (requestCode == REQUEST_CAMERA1) {
imageView = (ImageView) findViewById(R.id.postImageView1);
bm = MediaStore.Images.Media.getBitmap(getContentResolver(), tempImageUri);
//bm = BitmapFactory.decodeFile(tempImageUri.getPath());
imageFile = Storage.bitmapToFile(this, bm, "postFile1.jpg", imageWidth, imageHeight);
myFile=imageFile;
}
我犯的错误是:
java.io.FileNotFoundException:没有这样的文件或目录
在com.example.myapp.positemActivity.onActivityResult(positemActivity.java:478)->bm=MediaStore.Images.Media.getBitmap(getContentResolver(),tempImageUri)
E/AndroidRuntime:致命异常:主
java.lang.RuntimeException:无法将结果ResultInfo{who=null,request=31,result=0,data=null}传递到活动{…myapp.positemActivity}:java.lang.NullPointerException摆脱
bm
。将imageFile
设置为tempFile
,或使用Java I/O将文件复制到postFile1.jpg
确保在保存的实例状态Bundle
中按住tempFile
。当位图已经是内部存储器中的文件时,读取位图并将其保存回文件是没有意义的。另外,检查resultCode
,查看它是否为RESULT\u OK
。一些摄像头应用程序将不接受内容Uri
方案进行额外输出
,因为即使是谷歌自己的摄像头应用程序直到9个月前才支持该方案。出现此错误是因为它无法从tempImageUri创建位图。这是否意味着MediaStore.EXTRA_输出不会将完整大小的捕获图像写入tempImageUri给定的文件?您必须自己查找。首先,检查您是否得到了结果\u OK
——如果没有,那么您不应该期待图像。其次,检查由tempFile
标识的文件是否存在。仅将Uri
用于EXTRA\u输出
。请记住,许多相机应用程序都有bug,例如总是忽略额外的_输出
tempFile.exists());/这将返回false
。那么这和你的文件提供商无关,是吗?我认为目录“my_images”一开始就不存在。我看不到检查它是否存在以及是否创建它的代码。tempFile.createNewFile()代码>。不要自己创建文件!您应该只指定文件对象的路径。
tempFile = null;
File imagePath = new File(getFilesDir(),"my_images");
tempFile = new File(imagePath, "temp.jpg");
try {
tempFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("PostItemActivity", "tempFile: " + tempFile.exists()); // This returns false!!!
if (tempFile != null) {
tempImageUri = FileProvider.getUriForFile(PostItemActivity.this,
"com.example.myapp.fileprovider", tempFile);
//tempImageUri = Uri.fromFile(tempFile);
Log.d("PostItemActivity", "tempFile: " + tempFile);
Log.d("PostItemActivity", "tempFile uri: " + tempImageUri);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempImageUri);
startActivityForResult(intent, REQUEST_CAMERA1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView imageView=null;
Bitmap bm=null;
try {
if (requestCode == REQUEST_CAMERA1) {
imageView = (ImageView) findViewById(R.id.postImageView1);
bm = MediaStore.Images.Media.getBitmap(getContentResolver(), tempImageUri);
//bm = BitmapFactory.decodeFile(tempImageUri.getPath());
imageFile = Storage.bitmapToFile(this, bm, "postFile1.jpg", imageWidth, imageHeight);
myFile=imageFile;
}