在Jellybean中,使用FileProvider时,相机意图返回RESULT.CANCELLED

问题描述 投票:1回答:2

我跟着official docs在我的应用程序中执行相机意图。它适用于5.0多种设备,但不适用于Jellybean。

在4.1(JB)到4.4(KK)中,本机相机应用程序在捕获图像后显示“不幸的是,相机已停止错误”。在我的应用程序的onActivityResult中,返回的结果始终是RESULT.CANCELLED。我究竟做错了什么?

附:如果我不使用fileprovider并使用Uri.fromFile(file)获取uri,则相机意图在JB中正常工作。

以下是我的代码

// MainActivity.java
File f = PhotoHelper.createImageFile(this);
photoPath = "file:" + f.getAbsolutePath();
Uri photoURI = FileProvider.getUriForFile(this, "com.myapp.fileprovider", f);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, Constants.NEW_TOPIC_VIA_PHOTO);

// PhotoHelper.java
public static File createImageFile(Context context) throws IOException {
   String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
   String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
   File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
   return File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, storageDir);
}

// AndroidManifest.xml
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.myapp.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
      <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/file_paths"/>
</provider>

// file_paths.xml
<paths>
    <external-path
        name="App_Images"
        path="Android/data/com.myapp/files/Pictures"/>
</paths>
android android-camera android-4.2-jelly-bean android-camera-intent android-fileprovider
2个回答

0
投票

我为Android 4.3 Jelly Bean和Android 8 Oreo执行此操作:

void startCamera() {
        try {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)  {
                dispatchTakePictureIntent();
            }
            else
            {
                boolean focus = getPackageManager().hasSystemFeature( "android.hardware.camera.autofocus");
                //displayMsg(context, "In startCamera AutoFocus = " + focus);
                dispatchTakePictureIntent4();
            }
        } catch (IOException e) {
            displayMsg(context, "In startCamera " + e.toString());

        }
    }

 public void dispatchTakePictureIntent() throws IOException {

        try {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    displayMsg(context, "Error occurred while creating the File " + ex.toString());
                    return;
                }

                // Continue only if the File was successfully created
                if (photoFile != null) {
                    //rww OLD here
                    //Uri photoURI = Uri.fromFile(createImageFile());

                    Uri photoURI = FileProvider.getUriForFile(TellYourStoryActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            createImageFile());

                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//added RWW
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);


                }

            }

        } catch (Exception e) {

            displayMsg(context, "dispatchTakePictureIntent " + e.toString());

        }

    }

    public void dispatchTakePictureIntent4() throws IOException {
        try {
            // MainActivity.java
            File f = this.createImageFile4(this);
            if (f == null)
            {
                displayMsg(context, "dispatchTakePictureIntent 444" + " file f is null");
                return;
            }
            String photoPath = "file:" + f.getAbsolutePath();
            mCurrentPhotoPath = "file:" + f.getAbsolutePath();
            mCurrentAbsolutePhotoPath = f.getAbsolutePath();
            //displayMsg(context, "dispatchTakePictureIntent 444 photopath\n" + photoPath);

            //Uri photoURI = FileProvider.getUriForFile(this, "com.myapp.fileprovider", f);

            Uri photoURI = Uri.fromFile(f);
            //displayMsg(context, "dispatchTakePictureIntent 444 uri" + photoURI.toString());
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, NEW_TOPIC_VIA_PHOTO);
        } catch (Exception e) {

            displayMsg(context, "dispatchTakePictureIntent 444" + e.toString());

        }
    }

    // PhotoHelper.java
    public  File createImageFile4(Context context) throws IOException {
        try {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
            String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
            File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            return File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, storageDir);
        } catch (Exception e) {

            displayMsg(context, "createImageFile4 444" + e.toString());

        }
        return null;
    }

 public File createImageFile() throws IOException {
        // Create an image file name
        File image = null;
        //displayMsg(context, "In createImageFile  ");
        try {


            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM), "Camera");
            image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            mCurrentPhotoPath = "file:" + image.getAbsolutePath();
            RWWmCurrentPhotoPath = "content:/" + image.getAbsolutePath();
            mCurrentAbsolutePhotoPath = image.getAbsolutePath();
        } catch (Exception e) {
            displayMsg(context, "In createImageFile  " + e.toString());
        }
        return image;
    }
© www.soinside.com 2019 - 2024. All rights reserved.