From 73e36320e7d418f8054154dc6ee67ad996b286a8 Mon Sep 17 00:00:00 2001 From: Thiago Filipe Soares da Rocha Date: Thu, 9 Feb 2017 13:52:31 -0300 Subject: [PATCH] Share Video Issue --- Assets/Plugins/Android/AndroidManifest.xml | 2 +- Assets/Plugins/Android/file_paths.xml | 5 +++++ Assets/Plugins/Android/file_paths.xml.meta | 8 ++++++++ Assets/Scenes/Main.unity | 2 +- Assets/Scripts/Export Video/AndroidPath.cs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Assets/Scripts/Export Video/AndroidPath.cs.meta | 12 ++++++++++++ Assets/Scripts/Export Video/ExportVideo.cs | 33 +++++++++++++++++++++++---------- Assets/Scripts/Export Video/MediaShareAndroid.cs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- Assets/Scripts/UI/ScreenManager.cs | 11 +++++++++-- ProjectSettings/ProjectSettings.asset | 2 +- 10 files changed, 191 insertions(+), 38 deletions(-) create mode 100644 Assets/Plugins/Android/file_paths.xml create mode 100644 Assets/Plugins/Android/file_paths.xml.meta create mode 100644 Assets/Scripts/Export Video/AndroidPath.cs create mode 100644 Assets/Scripts/Export Video/AndroidPath.cs.meta diff --git a/Assets/Plugins/Android/AndroidManifest.xml b/Assets/Plugins/Android/AndroidManifest.xml index 89c3a4c..0b22c86 100644 --- a/Assets/Plugins/Android/AndroidManifest.xml +++ b/Assets/Plugins/Android/AndroidManifest.xml @@ -24,6 +24,6 @@ - + \ No newline at end of file diff --git a/Assets/Plugins/Android/file_paths.xml b/Assets/Plugins/Android/file_paths.xml new file mode 100644 index 0000000..9202b04 --- /dev/null +++ b/Assets/Plugins/Android/file_paths.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Assets/Plugins/Android/file_paths.xml.meta b/Assets/Plugins/Android/file_paths.xml.meta new file mode 100644 index 0000000..6d8385a --- /dev/null +++ b/Assets/Plugins/Android/file_paths.xml.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3efe7884117ce2a42ab53ef3041426fc +timeCreated: 1486638270 +licenseType: Free +TextScriptImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index c1dc692..2d8dd45 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -793,7 +793,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!224 &160532052 RectTransform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Export Video/AndroidPath.cs b/Assets/Scripts/Export Video/AndroidPath.cs new file mode 100644 index 0000000..8d7e577 --- /dev/null +++ b/Assets/Scripts/Export Video/AndroidPath.cs @@ -0,0 +1,63 @@ +using UnityEngine; +using System.Collections; +using System; + + +public class AndroidPaths : MonoBehaviour { + + public static string GetAndroidContextExternalFilesDir + { + get + { + string path = ""; + + if (Application.platform == RuntimePlatform.Android) + { + try + { + using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + { + using (AndroidJavaObject ajo = ajc.GetStatic("currentActivity")) + { + path = ajo.Call("getExternalFilesDir", null).Call("getAbsolutePath"); + } + } + } + catch (Exception e) + { + Debug.LogWarning("Error fetching native Android external storage dir: " + e.Message); + } + } + return path; + } + } + + public static string GetAndroidContextInternalFilesDir + { + get + { + string path = ""; + + if (Application.platform == RuntimePlatform.Android) + { + try + { + using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + { + using (AndroidJavaObject ajo = ajc.GetStatic("currentActivity")) + { + path = ajo.Call("getFilesDir").Call("getAbsolutePath"); + } + } + } + catch (Exception e) + { + Debug.LogWarning("Error fetching native Android internal storage dir: " + e.Message); + } + } + return path; + } + } + + +} diff --git a/Assets/Scripts/Export Video/AndroidPath.cs.meta b/Assets/Scripts/Export Video/AndroidPath.cs.meta new file mode 100644 index 0000000..91e32b2 --- /dev/null +++ b/Assets/Scripts/Export Video/AndroidPath.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 71dacb9b6a7d04349b9913751ef87ad4 +timeCreated: 1486607668 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Export Video/ExportVideo.cs b/Assets/Scripts/Export Video/ExportVideo.cs index b343fec..93e5147 100644 --- a/Assets/Scripts/Export Video/ExportVideo.cs +++ b/Assets/Scripts/Export Video/ExportVideo.cs @@ -45,6 +45,7 @@ public class ExportVideo : MonoBehaviour { private float videoSize = 0; + private int bytes = 0; private string videoId = ""; private byte[] videoContent = null; @@ -157,21 +158,31 @@ public class ExportVideo : MonoBehaviour { Debug.Log("Sharing video content"); if(gloss == currentGloss) { - if (videoContent != null) - new MediaShareAndroid().ShareActivity(videoContent,currentTime); - else + try { - new AndroidToast().showToast("Erro ao compartilhar vídeo"); - screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + if (videoContent != null) + new MediaShareAndroid().ShareActivity(videoContent,bytes, currentTime); + else + { + new AndroidToast().showToast("Erro ao compartilhar vídeo"); + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + } + } + catch (Exception e) + { + new AndroidToast().showToast(e.Message); + new AndroidToast().showToast(e.StackTrace); } - }else + + } + else { currentGloss = gloss; currentTime = System.DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss"); if (videoContent != null) - new MediaShareAndroid().ShareActivity(videoContent, currentTime); + new MediaShareAndroid().ShareActivity(videoContent,bytes, currentTime); else { screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); @@ -187,7 +198,8 @@ public class ExportVideo : MonoBehaviour { private IEnumerator videoDownload() { videoDownloadRunning = true; - WWW DownloadVideo = new WWW(SERVER_URL + videoId); + // WWW DownloadVideo = new WWW(SERVER_URL + videoId); + WWW DownloadVideo = new WWW("http://caiomcg.com/rural.mp4"); while (!DownloadVideo.isDone) { screenManager.updateProgressDownloadSprite(DownloadVideo.progress); @@ -196,8 +208,9 @@ public class ExportVideo : MonoBehaviour { try { - if (DownloadVideo.bytesDownloaded > 0 && DownloadVideo.bytesDownloaded <= videoSize) + if (DownloadVideo.bytesDownloaded > 0 ) { + bytes = DownloadVideo.bytesDownloaded; videoContent = DownloadVideo.bytes; OnDownloadFinished(); } @@ -293,7 +306,7 @@ public class ExportVideo : MonoBehaviour { public IEnumerator WaitForResponse(WWW www, Events.RequestSuccess success, Events.RequestError error) { - yield return Methods.WaitForResponse(www, 60f, success, error); + yield return Methods.WaitForResponse(www, 60*5f, success, error); } diff --git a/Assets/Scripts/Export Video/MediaShareAndroid.cs b/Assets/Scripts/Export Video/MediaShareAndroid.cs index 2114933..be05cf4 100644 --- a/Assets/Scripts/Export Video/MediaShareAndroid.cs +++ b/Assets/Scripts/Export Video/MediaShareAndroid.cs @@ -17,6 +17,7 @@ using UnityEngine; using System.Collections; using System.IO; +using System; @@ -24,37 +25,32 @@ public class MediaShareAndroid : MonoBehaviour { private string MediaType = "video/*"; - private string TitleMessage = "Compartilhar Vídeo"; + private string SubtitleMessage = "VLibras"; private string AndroidPath = AndroidJavaUti.CurrentSDCardPath; - private string Vlibras = "/VLibras"; - private string FileName = "/VLibrasVideo_"; - - - /*Makes sure that Vlibras Folder gets created and write the video bytes in it.*/ - - private void WriteOnAndroid(byte[] videoContent) - { - if(!Directory.Exists(AndroidPath + Vlibras)) - { - Directory.CreateDirectory(AndroidPath + Vlibras); - } - if (!Directory.Exists(AndroidPath + Vlibras + FileName)) - File.WriteAllBytes(AndroidPath + Vlibras + FileName, videoContent); + + private string destination = ""; + private string Vlibras = "VLibras"; + private string FileName = "VLibrasVideo_"; + private byte[] videoContent; + private int bytes; - } + /*Makes sure that Vlibras Folder gets created and write the video bytes in it.*/ /* Creates the sharing activity and shows the chooser to the user*/ - public void ShareActivity(byte[] videoContent, string CurrentTime) + public void ShareActivity(byte[] videoContent,int bytes, string CurrentTime) { #if UNITY_ANDROID + this.videoContent = videoContent; + this.bytes = bytes; FileName += CurrentTime + ".mp4"; - WriteOnAndroid(videoContent); + ShareIntent(); + /* // Create Refernece of AndroidJavaClass class for intent AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); // Create Refernece of AndroidJavaObject class intent @@ -67,18 +63,18 @@ public class MediaShareAndroid : MonoBehaviour //Set title of action or intent intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TITLE"), TitleMessage); - /* + /* // Set actual data which you want to share intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TEXT"), Media); AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); // Invoke android activity for passing intent to share data currentActivity.Call("startActivity", intentObject); - */ + AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); - AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", AndroidPath + Vlibras + FileName);// Set Image Path Here + AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", Path.Combine(AndroidPath + Vlibras, FileName));// Set Image Path Here AndroidJavaObject uriObject = uriClass.CallStatic("fromFile", fileObject); // string uriPath = uriObject.Call("getPath"); bool fileExist = fileObject.Call("exists"); @@ -89,10 +85,59 @@ public class MediaShareAndroid : MonoBehaviour AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); currentActivity.Call("startActivity", intentObject); - new AndroidToast().showToast("Vídeo salvo em /Vlibras"); - + + */ + #endif } + + private void ShareIntent() + { + destination = Path.Combine(AndroidPath, Vlibras); + DirectoryInfo dir = new DirectoryInfo(destination); + new AndroidToast().showToast(destination); + if (!dir.Exists) + { + dir.Create(); + } + + dir = new DirectoryInfo(Path.Combine(destination, FileName)); + if (!dir.Exists) + { + FileStream SourceStream = new FileStream(Path.Combine(destination, FileName), FileMode.OpenOrCreate); + if (SourceStream.CanWrite) + { + SourceStream.Write(videoContent, 0, bytes); + SourceStream.Close(); + + } + else + { + new AndroidToast().showToast("Erro ao escrever arquivo"); + } + + } + + + // block to open the file and share it ------------START + AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); + AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); + intentObject.Call("setAction", intentClass.GetStatic("ACTION_SEND")); + AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); + AndroidJavaObject uriObject = uriClass.CallStatic("parse", "file://" + Path.Combine(destination,FileName)); + intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_STREAM"), uriObject); + intentObject.Call("setType", MediaType); + //add data to be passed to the other activity i.e., the data to be sent + intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_SUBJECT"), SubtitleMessage); + intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TEXT"), "Vlibras Video"); + intentObject.Call("setType", "video/mp4"); + AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); + AndroidJavaObject jChooser = intentClass.CallStatic("createChooser", intentObject, "Compartilhar via"); + AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); + currentActivity.Call("startActivity", jChooser); + + + } } diff --git a/Assets/Scripts/UI/ScreenManager.cs b/Assets/Scripts/UI/ScreenManager.cs index 5aa129b..79142f4 100644 --- a/Assets/Scripts/UI/ScreenManager.cs +++ b/Assets/Scripts/UI/ScreenManager.cs @@ -33,6 +33,7 @@ public class ScreenManager : MonoBehaviour { public GameObject downloadProgressLayer; private bool onLockExport = false; + private bool onLockShare = false; @@ -333,7 +334,7 @@ public class ScreenManager : MonoBehaviour { setTranslateButtonActive( ! playing); setPauseMenuState(playing && paused); setRepeatLayerState( ! playing && repeatable); - if(!onLockExport) + if(!onLockExport && !onLockShare) changeExportStates(ExportLayers.ExportLayer.All, !playing && repeatable); @@ -345,28 +346,33 @@ public class ScreenManager : MonoBehaviour { switch (layers) { case ExportLayers.ExportLayer.Export_Layer: + onLockShare = false; onLockExport = false; setExportContainerState(show_Layer); setExportLayerState(show_Layer); break; case ExportLayers.ExportLayer.Progress_Layer: onLockExport = true; + onLockShare = true; setExportContainerState(show_Layer); setProgressLayerState(show_Layer); break; case ExportLayers.ExportLayer.Download_Layer: onLockExport = true; + onLockShare = true; setExportContainerState(show_Layer); setDownloadLayerState(show_Layer); break; case ExportLayers.ExportLayer.Progress_Download_Layer: onLockExport = true; + onLockShare = true; setExportContainerState(show_Layer); setDownloadProgressLayerState(show_Layer); break; case ExportLayers.ExportLayer.Share_Layer: - onLockExport = true; + onLockExport = true; + onLockShare = false; setExportContainerState(show_Layer); setShareLayerState(show_Layer); break; @@ -374,6 +380,7 @@ public class ScreenManager : MonoBehaviour { onLockExport = show_Layer; break; default: + onLockShare = false; onLockExport = false; setExportContainerState(show_Layer); setExportLayerState(show_Layer); diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 7305ee7..226e8aa 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -111,7 +111,7 @@ PlayerSettings: iPhoneScriptCallOptimization: 0 iPhoneBuildNumber: 0 ForceInternetPermission: 0 - ForceSDCardPermission: 0 + ForceSDCardPermission: 1 CreateWallpaper: 0 APKExpansionFiles: 0 preloadShaders: 0 -- libgit2 0.21.2