Android:无法加载从相机保存的照片

时间:2019-10-30 10:10:30

标签: android android-intent camera uri

我在用相机拍摄照片并将其显示在活动上时遇到问题。基本上,不会加载照片,并且BitmapFactory.decodeStream返回null。奇怪的是,如果我在调试中等待了一段时间,它就会被加载。尽管此行为不一致。我还使用com.isseiaoki.simplecropview.CropImageView裁剪处理位图解码的图像。这是相关代码:

活动:

private final String DATA_PATH = "/mnt/sdcard/ocr/";
private void init() {
            if (getIntent().getIntExtra(KEY_SHOULD_OPEN, 0) == CAMERA) {
                tryOpenCamera();
            } else {
                openGallery();
            }

            createImageFile();
        }

    public void createImageFile(){
            File photo;
            try
            {
                // place where to store camera taken picture
                photo = createTemporaryFile("picture", ".jpg");
            }
            catch(Exception e)
            {
                showGenericErrorDialog();
                photo = null;
            }
            if(photo != null){

                photoUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", photo);
            }
        }


        private File createTemporaryFile(String part, String ext) throws Exception
        {
            File tempDir=new File(DATA_PATH);

            if(!tempDir.exists())
            {
                tempDir.mkdirs();
            }
            return File.createTempFile(part, ext, tempDir);
        }

    private void openCamera() {
            Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
                updateImageUI(photoUri);
            } 
        }

    private void updateImageUI(Uri uri) {
            getBinding().cropImageView.startLoad(
                    uri,
                    new LoadCallback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {
                            showGenericErrorDialog();
                        }
                    });
        }

与作物库相关的代码:

public void startLoad(Uri sourceUri, LoadCallback callback) {
        mLoadCallback = callback;
        mSourceUri = sourceUri;
        if (sourceUri == null) {
            postErrorOnMainThread(mLoadCallback);
            throw new IllegalStateException("Source Uri must not be null.");
        }
        mExecutor.submit(new Runnable() {
            @Override
            public void run() {
                mIsLoading = true;
                mExifRotation = Utils.getExifOrientation(getContext(), mSourceUri);
                int maxSize = Utils.getMaxSize();
                int requestSize = Math.max(mViewWidth, mViewHeight);
                if (requestSize == 0) requestSize = maxSize;
                try {
                    final Bitmap sampledBitmap = Utils.decodeSampledBitmapFromUri(getContext(),
                            mSourceUri,
                            requestSize);
                    mInputImageWidth = Utils.sInputImageWidth;
                    mInputImageHeight = Utils.sInputImageHeight;
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mAngle = mExifRotation;
                            setImageBitmap(sampledBitmap);
                            if (mLoadCallback != null) mLoadCallback.onSuccess();
                            mIsLoading = false;
                        }
                    });
                } catch (OutOfMemoryError e) {
                    Logger.e("OOM Error: " + e.getMessage(), e);
                    postErrorOnMainThread(mLoadCallback);
                    mIsLoading = false;
                } catch (Exception e) {
                    Logger.e("An unexpected error has occurred: " + e.getMessage(), e);
                    postErrorOnMainThread(mLoadCallback);
                    mIsLoading = false;
                }
            }
        });
    }

public static Bitmap decodeSampledBitmapFromUri(Context context, Uri sourceUri, int requestSize) {
        InputStream is = null;
        try {
            is = context.getContentResolver().openInputStream(sourceUri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = Utils.calculateInSampleSize(context, sourceUri, requestSize);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(is, null, options);
    }

清单:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

请注意,URI看起来像这样:content://my.package.provider/%2Fmnt%2Fsdcard%2F/ocr/picture9052876345621346933.jpg,并且我正在使用Android 8的三星Galaxy S7进行测试

编辑:到目前为止,问题仅在于S7,其他设备正常工作

知道为什么会发生这种情况以及如何避免这种情况吗?

0 个答案:

没有答案