- Home /
Unity editor sending screenshot to my bucket but Android build not sending screenshot.
Hi, I am able to send screenshot to bucket using the code in Editor. However my build is not sending the file. I am attaching the error from android log below:
2020-09-11 16:03:48.811 573 573 Error SELinux avc: denied { find } for interface=vendor.qti.hardware.servicetracker::IServicetracker sid=u:r:system_server:s0 pid=1292 scontext=u:r:system_server:s0 tcontext=u:object_r:default_android_hwservice:s0 tclass=hwservice_manager permissive=0 2020-09-11 16:03:49.003 27757 27785 Error Unity IOException: Sharing violation on path /storage/emulated/0/Android/data/com.mycompany.buckettry/files_ss 2020-09-11 16:03:49.003 27757 27785 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in :0 2020-09-11 16:03:49.003 27757 27785 Error Unity at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in :0 2020-09-11 16:03:49.003 27757 27785 Error Unity at System.IO.File.WriteAllBytes (System.String path, System.Byte[] bytes) [0x00000] in :0 2020-09-11 16:03:49.003 27757 27785 Error Unity at AWSSDK.Examples._mys3+<sendscreenshot>d_21.MoveNext () [0x00000] in :0 2020-09-11 16:03:49.003 27757 27785 Error Unity at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in :0
Below is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.Runtime;
using System.IO;
using System;
using Amazon.S3.Util;
using System.Collections.Generic;
using Amazon.CognitoIdentity;
using Amazon;
namespace AWSSDK.Examples
{
public class _mys3 : MonoBehaviour
{
public string IdentityPoolId = "";
public string CognitoIdentityRegion = RegionEndpoint.USEast1.SystemName;
private RegionEndpoint _CognitoIdentityRegion
{
get { return RegionEndpoint.GetBySystemName(CognitoIdentityRegion); }
}
public string S3Region = RegionEndpoint.USEast1.SystemName;
private RegionEndpoint _S3Region
{
get { return RegionEndpoint.GetBySystemName(S3Region); }
}
public string S3BucketName = null;
public string SampleFileName = null;
public Button PostBucketButton = null;
public Button GetObjectButton = null;
public Text ResultText = null;
void Start()
{
UnityInitializer.AttachToGameObject(this.gameObject);
PostBucketButton.onClick.AddListener(() => { PostObject(); });
GetObjectButton.onClick.AddListener(() => { GetObject(); });
// Fixes: "InvalidOperationException: Cannot override system-specified headers"
AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
}
#region private members
private IAmazonS3 _s3Client;
private AWSCredentials _credentials;
private AWSCredentials Credentials
{
get
{
if (_credentials == null)
_credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoIdentityRegion);
return _credentials;
}
}
private IAmazonS3 Client
{
get
{
if (_s3Client == null)
{
_s3Client = new AmazonS3Client(Credentials, _S3Region);
}
//test comment
return _s3Client;
}
}
#endregion
/// <summary>
/// Get Object from S3 Bucket
/// </summary>
private void GetObject()
{
ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
{
string data = null;
var response = responseObj.Response;
if (response.ResponseStream != null)
{
using (StreamReader reader = new StreamReader(response.ResponseStream))
{
data = reader.ReadToEnd();
}
ResultText.text += "\n";
ResultText.text += data;
}
});
}
/// <summary>
/// Post Object to S3 Bucket.
/// </summary>
public void PostObject()
{
StartCoroutine(_sendscreenshot());
}
public IEnumerator _sendscreenshot()
{
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
// Convert.ToBase64String(bytes);
Destroy(tex);
File.WriteAllBytes(Application.persistentDataPath + "_ss" , bytes);
//var stream = new FileStream(Application.persistentDataPath + "_ss", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
// var stream = new FileStream(Application.persistentDataPath + Path.DirectorySeparatorChar + bytes, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
var stream = new FileStream(Application.persistentDataPath + "_ss" , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
var request = new PostObjectRequest()
{
Bucket = S3BucketName,
Key = SampleFileName,
InputStream = stream,
CannedACL = S3CannedACL.PublicReadWrite,
Region = _S3Region
};
ResultText.text += "\nMaking HTTP post call";
Client.PostObjectAsync(request, (responseObj) =>
{
if (responseObj.Exception == null)
{
ResultText.text += string.Format("\nobject {0} posted to bucket {1}", responseObj.Request.Key, responseObj.Request.Bucket);
}
else
{
ResultText.text += "\nException while posting the result object";
// Changed from sample so we can see actual error and not null pointer exception
ResultText.text += string.Format("\n receieved error {0}", responseObj.Exception.ToString());
}
});
}
private string GetPostPolicy(string bucketName, string key, string contentType)
{
bucketName = bucketName.Trim();
key = key.Trim();
// uploadFileName cannot start with /
if (!string.IsNullOrEmpty(key) && key[0] == '/')
{
throw new ArgumentException("uploadFileName cannot start with / ");
}
contentType = contentType.Trim();
if (string.IsNullOrEmpty(bucketName))
{
throw new ArgumentException("bucketName cannot be null or empty. It's required to build post policy");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("uploadFileName cannot be null or empty. It's required to build post policy");
}
if (string.IsNullOrEmpty(contentType))
{
throw new ArgumentException("contentType cannot be null or empty. It's required to build post policy");
}
string policyString = null;
int position = key.LastIndexOf('/');
if (position == -1)
{
policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" +
bucketName + "\"},[\"starts-with\", \"$key\", \"" + "\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + contentType + "\"" + "]]}";
}
else
{
policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" +
bucketName + "\"},[\"starts-with\", \"$key\", \"" + key.Substring(0, position) + "/\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + contentType + "\"" + "]]}";
}
return policyString;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dehashing a password for logging in 1 Answer
uploading screenshot to server 3 Answers
Post Reqest with dictionary 1 Answer