某些设备上使用ACTION_IMAGE_CAPTURE拍摄照片时应用会崩溃吗?

时间:2017-10-10 08:08:38

标签: android android-camera

我的应用程序面临一些奇怪的行为:在某些设备上,如samsung索尼Onplus,我的应用程序工作正常,但有些设备如

  1. Lenovo a7000
  2. redmi x
  3. 应用程序崩溃,我收到此enter image description here错误

    我的代码是:

    private void captureImageFromCamera() {
    
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
            photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (photoFile != null) {
    
                Uri photoURI;
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
                    photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                } else {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                }
    
                startActivityForResult(takePictureIntent, Constant.REQUEST_TAKE_PHOTO);
            }
        }
    
    }
    

    我在这里创建图像文件

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imageFileName = "IMAGE_" + timeStamp + "_";
    
        //  File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES + "/PhotoGallery/");
    
        File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES );
    
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );
    
        mCurrentPhotoPath = image.getAbsolutePath();
    
        Log.d("imageFileName", imageFileName);
        Log.d("imageFile", image.toString());
    
        return image;
    
    }
    

    这是我的OnActivityResult:

    public void onActivityResult(int requestCode,int resultCode,Intent data){

    if (requestCode == Constant.REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
            try {
                setPic();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    }
    

    }

    在这里,我通过发送到另一个活动来设置Pic:

    private void setPic() throws IOException {
    
        Intent intent = new Intent(MainActivity.this, EditImageActivity.class);
    
        intent.putExtra("mCurrentPhotoPath", compressImage(mCurrentPhotoPath,MainActivity.this));
    
    
    
        intent.putExtra("CalledBy","Camera");
        startActivity(intent);
    
    }
    

    还有另一个人面临同样的问题。找到答案了。我在stackoverflow上累了很多问题但是没有对我有用, 请让我知道这个问题,为什么有些设备工作正常,我上面提到的一些设备没有, 感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我找到了两种解决您的问题的方法1st One,而2nd One在下面

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Create a File reference to access to future access
    photoFile = getPhotoFileUri(photoFileName);  

    // wrap File object into a content provider
    // required for API >= 24
    // See https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
    Uri fileProvider = FileProvider.getUriForFile(MyActivity.this, "com.codepath.fileprovider", photoFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider); 

    // If you call startActivityForResult() using an intent that no app can handle, your app will crash.
    // So as long as the result is not null, it's safe to use the intent.
    if (intent.resolveActivity(getPackageManager()) != null) {
        // Start the image capture intent to take photo
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

其中

public File getPhotoFileUri(String fileName) {
    // Get safe storage directory for photos
    // Use `getExternalFilesDir` on Context to access package-specific directories.
    // This way, we don't need to request external read/write runtime permissions.
    File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
       Log.d(APP_TAG, "failed to create directory");
    }

    // Return the file target for the photo based on filename
    File file = new File(mediaStorageDir.getPath() + File.separator + fileName);

    return file;
}

我也面临这个问题。新设备需要FLAG_GRANT_READ_URI_PERMISSIONFLAG_GRANT_WRITE_URI_PERMISSION,对于具有 Api> 24 的设备,您必须实现provider