question

AJ avatar image
AJ asked

Upload Photo from Mobile Gallery and save it to Playfab files

Hi there,

I am trying to upload a profile photo from the phone's gallery but I am getting "PlayerPhoto (pending file upload, cannot download)". Could you please help me with that?

Here is my code:

public void PickPhoto(int maxSize)
{
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
        Debug.Log("Image path: " + path);
        if (path != null)
        {
            // Create Texture from selected image
            Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
            // Assign the texture to the screen photo
            _profilePhoto.texture = texture;
            
            if (texture == null)
            {
                Debug.Log("Couldn't load texture from " + path);
                return;
            }
        }
    }, "Select a PNG image", "image/png");

    Debug.Log("Permission result: " + permission);
}

public void UploadFile(string fileName)
{
    if (GlobalFileLock != 0)
        throw new Exception("This example overly restricts file operations for safety. Careful consideration must be made when doing multiple file operations in parallel to avoid conflict.");

    ActiveUploadFileName = fileName;

    GlobalFileLock += 1; // Start InitiateFileUploads
    var request = new PlayFab.DataModels.InitiateFileUploadsRequest
    {
        Entity = new PlayFab.DataModels.EntityKey { Id = entityId, Type = entityType },
        FileNames = new List<string> { ActiveUploadFileName },
    };
    PlayFabDataAPI.InitiateFileUploads(request, OnInitFileUpload, OnInitFailed);
}

void OnInitFileUpload(PlayFab.DataModels.InitiateFileUploadsResponse response)
{
    Texture2D tex = (Texture2D)_profilePhoto.texture;
    tex.ReadPixels(new Rect(0, 0, _profilePhoto.texture.width, _profilePhoto.texture.height), 0, 0);
    tex.Apply();
    byte[] bytes = tex.EncodeToPNG();

    GlobalFileLock += 1; // Start SimplePutCall
    PlayFabHttp.SimplePutCall(response.UploadDetails[0].UploadUrl,
        bytes,
        FinalizeUpload,
        error => { Debug.Log(error); }
    );
    GlobalFileLock -= 1; // Finish InitiateFileUploads
}

void FinalizeUpload(byte[] obj)
{
    GlobalFileLock += 1; // Start FinalizeFileUploads
    var request = new PlayFab.DataModels.FinalizeFileUploadsRequest
    {
        Entity = new PlayFab.DataModels.EntityKey { Id = entityId, Type = entityType },
        FileNames = new List<string> { ActiveUploadFileName },
    };
    PlayFabDataAPI.FinalizeFileUploads(request, OnUploadSuccess, OnSharedFailure);
    GlobalFileLock -= 1; // Finish SimplePutCall
}


void OnUploadSuccess(PlayFab.DataModels.FinalizeFileUploadsResponse result)
{
    Debug.Log("File upload success: " + ActiveUploadFileName);
    GlobalFileLock -= 1; // Finish FinalizeFileUploads
}
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sarah Zhang avatar image Sarah Zhang commented ·

We will dig into it.

0 Likes 0 ·
AJ avatar image AJ Sarah Zhang commented ·

Thank you very much for you quick reply!

0 Likes 0 ·
Sarah Zhang avatar image
Sarah Zhang answered

We tried to test your code in the Unity Editor (replaced the photo into the plain text), it looks like your uploading method works fine. And we can’t reproduce the error -- “pending file upload, cannot download”, did you customize this error message? If so, what is the error returned by PlayFab?

If the error is “/File/InitiateFileUploads: An operation is already pending for this file, you must either complete that operation or abort it before performing a new operation.”, it means the API FinalizeFileUploads didn't be called successfully after you upload the file. In this case, you need to check this method, especially check that if the bytes are null or not.

 PlayFabHttp.SimplePutCall(response.UploadDetails[0].UploadUrl,
            bytes,
            FinalizeUpload,
            error => { Debug.Log(error); }
        );
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

tobby avatar image
tobby answered

i have to same this. can share all code? please

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.