文件中加载的图像并不总是出现在ImageView中

时间:2014-02-09 23:42:17

标签: android image android-imageview

我从ImageView加载的File包含在PopupWindow的{​​{1}}中。图像并不总是显示出来。这似乎是随机的。

图像应该来自拍摄的图像。我知道文件写得正确,因为当我进入下一个活动并从同一个文件加载另一个ImageView时,它显示没问题。

请注意,此Fragment还有另一个Fragment,但不在ImageView中,并且在我拍摄照片后总会显示。我怀疑它必须在PopupWindow中做些什么。

为什么它只是随机加载ImageView?

从PopupWindow中的File加载的ImageView

PopupWindow

启动相机& OnActivityResult

File file = new File(getActivity().getExternalFilesDir(null), step.getExtraImageFilename());
            FileInputStream fis = new FileInputStream(file);
            Bitmap imgFromFile = BitmapFactory.decodeStream(fis);
            extraImage.setImageBitmap(imgFromFile);
            extraImage.invalidate();
            fis.close();

权限

private void startCameraActivity(int requestCode) {
    ImageHandler imageHandler = new ImageHandler(getActivity());
    String filename = "";

    switch (requestCode) {
    case REQUEST_PICTURE:
        filename = imageHandler.getImageFilename(step.getChecklistId(), step.getOrder(), false);
        step.setImageFilename(filename);
        break;
    case REQUEST_PICTURE_EXTRA:
        filename = imageHandler.getImageFilename(step.getChecklistId(), step.getOrder(), true);
        step.setExtraImageFilename(filename);
        break;
    default:
        break;
    }

    File file = new File(getActivity().getExternalFilesDir(null), filename);
    Uri outputFileUri = Uri.fromFile(file);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(intent, requestCode);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case REQUEST_PICTURE:
        if (resultCode == Activity.RESULT_OK) { 
            Log.i("IMAGE FILE WRITTEN", step.getImageFilename());
            ImageHandler.compressAndRotateImage(getActivity(), step.getImageFilename());

            finishStep();
            showResult();
            checkIfAllFinished();
            if (step.getIsAllFinished()) { ((StepActivity)getActivity()).goToNextStep(); }
        }
        break;

    case REQUEST_PICTURE_EXTRA:
        if (resultCode == Activity.RESULT_OK) {
            Log.i("IMAGE FILE WRITTEN", step.getExtraImageFilename());
            ImageHandler.compressAndRotateImage(getActivity(), step.getExtraImageFilename());

            if (step.getReqPicture()) { step.setIsReqPictureFinished(true); }
            showExtraPicture();
            checkIfAllFinished();
            if (step.getIsAllFinished()) { ((StepActivity)getActivity()).goToNextStep(); }
        }
        break;

    default:
        break;
    }
}

0 个答案:

没有答案