How to create Media entity and attach to user field I'm trying to import user profile pictures from Drupal 7 to Drupal 8. So far I've managed to upload the picture from the url and save it to the file system, create Media entity and use the user id as author. However, the picture doesn't show to the user profile (Manage Form Display), neither to the Edit Media Content Type page. 
Here is the code I have so far:
$values = array(
      'name' =&gt; $user[&quot;name&quot;],
      'pass' =&gt; [
         'value' =&gt; $user[&quot;pass&quot;],
         'pre_hashed' =&gt; TRUE,
      ],
      'status' =&gt; $user[&quot;status&quot;],
      'mail' =&gt; $user[&quot;mail&quot;],
      'field_first_name' =&gt; $user[&quot;field_first_name&quot;],
      'field_last_name' =&gt; $user[&quot;field_last_name&quot;],
);

$userStorage = \Drupal::entityTypeManager()-&gt;getStorage('user');
$userEntity = $userStorage-&gt;create($values);

    $replaced = str_replace(&quot;\\&quot;, &quot;&quot;, $user[&quot;picture&quot;]);
    $imageData = file_get_contents($replaced);
    $fileName = basename($replaced);
    $file = file_save_data(
        $imageData, 
        &quot;public://&quot;. $fileName, 
        FileSystemInterface::EXISTS_RENAME
    );
    
    $media = Media::create([
        'bundle' =&gt; 'image',
        'uid' =&gt; $userEntity-&gt;id(),
        'langcode' =&gt; \Drupal::languageManager()-&gt;getDefaultLanguage()-&gt;getId(),
        'field_media_file' =&gt; [
             'target_id' =&gt; $file-&gt;id(),
             'alt' =&gt; $file-&gt;getFilename(),
        ],
        'thumbnail' =&gt; [
             'target_id' =&gt; $file-&gt;id(),
             'alt' =&gt; $file-&gt;getFilename(),
        ],
    ]);
    
    $media-&gt;setName($fileName)-&gt;setPublished(TRUE)-&gt;save();
   
    $userEntity-&gt;field_user_picture-&gt;appendItem($media);

Is the something missing in the code?
UPDATE
I noticed (as also pointed out in the comments) $userEntity-&gt;save(); is missing after $userEntity-&gt;field_user_picture-&gt;appendItem($media);.
However, after I added that line, I get the following error:
Error: Call to undefined method Drupal\media\Entity\Media::getFileUri() in Drupal\image\Plugin\Field\FieldType\ImageItem-&gt;preSave() (line 321 of /var/www/html/web/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php) #0 /var/www/html/web/core/lib/Drupal/Core/Field/FieldItemList.php(233): Drupal\image\Plugin\Field\FieldType\ImageItem-&gt;preSave()

Is it related to file_save_data() function?
I've found the solution. 
I don't need to create a Media entity and append it to the User entity, since the profile picture field type is Image, so I just need to add the file-&gt;id() to the specific field. 
Here is the code:
$replaced = str_replace(&quot;\\&quot;, &quot;&quot;, $user[&quot;picture&quot;]);
$imageData = file_get_contents($replaced);
$fileName = basename($replaced);
$file = file_save_data(
      $imageData, 
      &quot;public://&quot;. $fileName, 
      FileSystemInterface::EXISTS_RENAME
);

$values = array(
      'name' =&gt; $user[&quot;name&quot;],
      'pass' =&gt; [
         'value' =&gt; $user[&quot;pass&quot;],
         'pre_hashed' =&gt; TRUE,
      ],
      'field_user_picture' =&gt; array('target_id' =&gt;$file-&gt;id()),
      'status' =&gt; $user[&quot;status&quot;],
      'mail' =&gt; $user[&quot;mail&quot;],
      'field_first_name' =&gt; $user[&quot;field_first_name&quot;],
      'field_last_name' =&gt; $user[&quot;field_last_name&quot;],
);

$userStorage = \Drupal::entityTypeManager()-&gt;getStorage('user');
$userEntity = $userStorage-&gt;create($values);

Then, if required, I can create the Media entity as following:
$media = Media::create([
          'bundle' =&gt; 'image',
          'uid' =&gt; $userEntity-&gt;id(),
          'langcode' =&gt; \Drupal::languageManager()-&gt;getDefaultLanguage()-&gt;getId(),
          'field_media_image' =&gt; [
             'target_id' =&gt; $file-&gt;id(),
             'alt' =&gt; $file-&gt;getFilename(),
          ],
          'thumbnail' =&gt; [
             'target_id' =&gt; $file-&gt;id(),
             'alt' =&gt; $file-&gt;getFilename(),
          ],
]);

$media-&gt;setName($fileName)-&gt;setPublished(TRUE)-&gt;save();