使用相机捕获后保存全尺寸照片会给onActivityResult resultCode 0

时间:2017-02-25 13:12:59

标签: android android-camera android-fileprovider

我试图用相机捕获后保存全尺寸照片但在onActivityResult上它给了我resultCode 0.我正在使用Fileprovider从filepaths.xml中的文件中获取Uri。您可以在下面看到我的代码:

在filepaths.xml中:

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="my_images/"/>
</paths>
AndroidManifest.xml中的

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
活动中的

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);

和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:没有这样的文件或目录 at com.example.myapp.PostItemActivity.onActivityResult(PostItemActivity.java:478) - &gt; bm = MediaStore.Images.Media.getBitmap(getContentResolver(),tempImageUri);

E / AndroidRuntime:致命异常:主要                                                                       java.lang.RuntimeException:将结果ResultInfo {who = null,request = 31,result = 0,data = null}传递给activity {... myapp.PostItemActivity}:java.lang.NullPointerException

0 个答案:

没有答案