An image taken from the camera doesn't always show up in my ImageView. It seems like it is random when it wants to show up. Why is it doing this?

Activity that takes picture

private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_PICTURE);
    }
}

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

    // Handles picture taking after finished
    if (requestCode == REQUEST_PICTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");

        ImageView imageResult = (ImageView) findViewById(R.id.result_image);
        imageResult.setImageBitmap(image);

        ImageHandler imageHandler = new ImageHandler(this);
        imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder());

        step.setImageFilename(imageHandler.getFilename(step.getChecklistId(), step.getOrder()));
        finishStep();
    }
}

XML to show picture

<ImageView 
            android:id="@+id/result_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:contentDescription="@string/taken_img_desc" />
有帮助吗?

解决方案

Are you using notifyDataSetChanged for your adapter? Is your view being refreshed? Try .invalidate();

其他提示

One more thing: You might experience that the camera intent does not return an image or an uri to an image. I created a wrapper to help you with that. See the whole discussion on stackoverflow here or find the code on github.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top