【问题标题】:App is crashing when taken picture using ACTION_IMAGE_CAPTURE on some devices?在某些设备上使用 ACTION_IMAGE_CAPTURE 拍照时应用程序崩溃?
【发布时间】:2017-10-10 08:08:38
【问题描述】:

我的应用程序遇到了一些奇怪的行为:在某些设备上,如三星索尼 Onplus,我的应用程序工作正常,但在某些设备上,如

  1. 联想 a7000
  2. 红米x

应用程序崩溃,我收到此 错误

我的代码是:

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的问题,但没有一个对我有用, 请让我知道这个问题,为什么在某些设备中可以正常工作而我上面提到的某些设备不能, 任何帮助表示赞赏

【问题讨论】:

  • 什么是Android版本的手机会崩溃??
  • 嘿@Xenolion 我上面提到的型号和版本是棉花糖,我提供所有运行时权限。
  • 在onActivityResult中添加super
  • @AnkitAman 你没明白我的意思我在某些设备上遇到问题 某些设备工作正常。
  • 是的,我确实正确添加了提供程序。我在上面提到了设备名称。

标签: android android-camera


【解决方案1】:

我找到了两个解决您的问题的方法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_PERMISSION 和 FLAG_GRANT_WRITE_URI_PERMISSION,对于具有 Api > 24 的设备,您必须实现 provider

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2015-06-10
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多