Search code examples
javaandroidimageandroid-fragmentsimageview

Why is my Android ImageView empty (loading up through Uri, Bitmap or using Picasso)


I have a fragment to display a queue of either videos of images. The video I display in the VideoView works fine, it replays, it's golden. But the images I put in the ImageView just appear invisible. I tried loading them through Uri, by reading a Bitmap, now they're set up with Picasso, and none of it fixed it. The AssetObtainer you'll see in MultimediaPlayer works with both sound files and videos so far, so I highly doubt it has an issue with images. Here's the code:

MultimediaPlayer.java :

public class MultimediaPlayer extends Fragment
{
    VideoView mVideoView;
    ImageView mImageView;
    MultimediaViewModel mMultimediaViewModel;
    Play mPlayThread;
    Activity mActivity;
    AssetObtainer assetObtainer = new AssetObtainer();
    public Long mTutorialId;
    public List<Multimedia> multimedias = new LinkedList<>();

    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment_multimedia_player, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        mActivity = requireActivity();
        mMultimediaViewModel = new ViewModelProvider(this).get(MultimediaViewModel.class);
        mVideoView = view.findViewById(R.id.video_embed);
        mImageView = view.findViewById(R.id.image_embed);
        getPlayer(0);
    }

    private void getPlayer(int position)
    {
        if(mPlayThread != null) {
            mPlayThread.interrupt();
            position++;
        }
        if(!multimedias.isEmpty()) {
            mPlayThread = new Play(multimedias.get(position));
            mPlayThread.start();
        }
    }

    private class Play extends Thread
    {
        private final Multimedia currentMedia;

        Play(Multimedia media){
            currentMedia = media;
        }

        @Override
        public void run()
        {
            int position = currentMedia.getPosition();
            int displayTime = currentMedia.getDisplayTime();
            boolean loopBool = currentMedia.getLoop();
            if(currentMedia.getType()) {
                mActivity.runOnUiThread(() -> {
                    mImageView.setVisibility(View.VISIBLE);
                    mVideoView.setVisibility(View.GONE);
                    try {
                        Picasso.get().load(assetObtainer.getFileFromAssets(requireContext(), currentMedia.getFullFileName())).into(mImageView);
                    } catch (IOException ignored) {}
                });
                if(displayTime>0) {
                    try {
                        sleep(displayTime);
                        if(!loopBool) multimedias.remove(currentMedia);
                        if(position<multimedias.size()-1) {
                            getPlayer(position);
                        } else getPlayer(0);
                    } catch (InterruptedException e) {
                        mActivity.runOnUiThread(() -> mImageView.setVisibility(View.GONE));
                        interrupt();
                    }
                }
            } else {
                mActivity.runOnUiThread(() -> {
                    mVideoView.setVisibility(View.VISIBLE);
                    mImageView.setVisibility(View.GONE);
                    try {
                        mVideoView.setVideoURI(Uri.fromFile(assetObtainer.getFileFromAssets(requireContext(), currentMedia.getFullFileName())));
                    } catch (IOException ignored) {}
                    if(loopBool && multimedias.size()==1) mVideoView.setOnCompletionListener(v->getPlayer(position-1));
                    mVideoView.start();
                });
            }
        }
    }

    @Override
    public void onPause()
    {
        if(mPlayThread!=null){
            mPlayThread.interrupt();
        }
        super.onPause();
    }
}

bed for the fragment in the activity .xml file :

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout_multimedia"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/active_instructions" />

and the .xml file of the fragment :

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".tutorial.mediaplayer.MultimediaPlayer">

    <VideoView
        android:id="@+id/video_embed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/image_embed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="@string/image_default_no_description" />

</androidx.constraintlayout.widget.ConstraintLayout>

As mentioned, video works super fine, but images won't show. I'd appreciate even monkey wrench suggestions before I have to rework this entirely.


Solution

  • As mentioned in my other answer, it's not related to the ImageView, it's a problem with SQLite and loading a boolean from the database. The default false which I wanted to correspond with video types is default, so it worked, but since it doesn't default to true the image condition wasn't fulfilled. Anyway the ImageView will work once it is actually ran.