0

So this question has been asked before, but please read to the end before down voting since this is different.

I have a camera which takes an image in Android and then returns that image as a bitmap to be placed into an ImageView. The image that is returned comes from a class that has done some work with a UVC camera and then returns the image. So when the Activity gets the image it gets it via a callback that was registered to the interface. In any case, here is the code of the callback.

@Override
    public void onCaptureComplete(final Bitmap data)
    {
        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                nextButton.setVisibility(View.VISIBLE);
                recaptureButton.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
                imageView.setImageBitmap(data);
                imageView.invalidate();

                ((View) mUVCCameraView).setVisibility(View.INVISIBLE);

                faceController.closeCamera();

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                data.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                imageData = byteArray;
            }
        });
    }

So the first time I get the image it sets it as it was captured, but if I recapture the image in the same instance of that Activity, so a recapture, then the image just shows the first image that was captured and never the second image. If I minimise the app and open it again then it crashes and tells me it is trying to reference a Bitmap image that does not exist anymore. The actual data is that of the new image though, it is just the ImageView that refuses to update.

I also tried using an AsyncTask to do this and have the exact same result.

@Override
public void onCaptureComplete(Bitmap data)
{
    new UpdateUI().execute(data);
}

public class UpdateUI extends AsyncTask<Bitmap, Void, Bitmap>
{
    @Override
    protected void onPreExecute()
    {

    }

    @Override
    protected Bitmap doInBackground(Bitmap... params)
    {
        return params[0];
    }

    @Override
    protected void onPostExecute(Bitmap data)
    {
        nextButton.setVisibility(View.VISIBLE);
        recaptureButton.setVisibility(View.VISIBLE);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageBitmap(data);
        imageView.invalidate();

        ((View) mUVCCameraView).setVisibility(View.INVISIBLE);

        faceController.closeCamera();

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        data.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        imageData = byteArray;
    }
}

So I was thinking I can try changing the Activity to use a fragment and see if that works or perhaps using a BroadCast receiver, but I am not sure why this is happening.

I thought that it might have to do with the face that the data is coming from a Static instance from the class/interface section of code but I have tried so many things and it just won't update.

Let me know if any more code is needed.

Thanks, Wihan

EDIT: The imageView.setVisibility happens but not the changing of the bitmap image.

  • I have now reworked the Activity to make use of a Fragment for the ImageView part. Then on each image captured I replace the old Fragment, still the ImageView does not update, I think there is something wrong with the ImageView class on my SDK or something, this makes no sense. I view the Bitmap in debug mode and I can see the image is different, but the ImageView shows the same image that was captured first. – Wihan Fourie Aug 30 '16 at 08:52

2 Answers2

0

You should really take a look at Picasso,

Picasso.with(getContext())
     .load(URL/URI)  (1)      /* Watch below for more details */
     .placeholder(R.drawable.ic_nothumb)
     .resize(120, 120)
     .into(iv);

(1) This Link explain how to load image from local storage

Community
  • 1
  • 1
Hugo Houyez
  • 470
  • 3
  • 19
  • Not really what I want to do, this is for getting an image over a network, I am capturing an image with the camera and want to update the ImageView... – Wihan Fourie Aug 29 '16 at 11:53
0

I have no idea why this was happening, but I managed to work around this by converting the image captured and stored on the internal storage, to a drawable. I then used imageView.setImageDrawable instead of setImageBitmap.