NativeGalleryDataProvider.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace NRKernal.NRExamples
  5. {
  6. public class NativeGalleryDataProvider
  7. {
  8. private static AndroidJavaClass m_NativeClass;
  9. public static AndroidJavaClass NativeClass
  10. {
  11. get
  12. {
  13. if (m_NativeClass == null)
  14. m_NativeClass = new AndroidJavaClass("ai.nreal.android.gallery.GalleryDataProvider");
  15. return m_NativeClass;
  16. }
  17. }
  18. public const string MAIN_ACTIVITY_CLASS = "com.unity3d.player.UnityPlayer";
  19. private static AndroidJavaObject m_CurrentActivity;
  20. public static AndroidJavaObject CurrentActivity
  21. {
  22. get
  23. {
  24. if (m_CurrentActivity == null)
  25. {
  26. using (AndroidJavaClass jc = new AndroidJavaClass(MAIN_ACTIVITY_CLASS))
  27. {
  28. m_CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
  29. }
  30. }
  31. return m_CurrentActivity;
  32. }
  33. }
  34. private AndroidJavaObject m_NativeObject;
  35. public NativeGalleryDataProvider()
  36. {
  37. m_NativeObject = new AndroidJavaObject("ai.nreal.android.gallery.GalleryDataProvider",
  38. CurrentActivity);
  39. }
  40. public void InsertImage(byte[] data, string displayName, string folderName)
  41. {
  42. InsertImageData(data, displayName, folderName);
  43. }
  44. public AndroidJavaObject InsertImageData(byte[] data, string displayName, string folderName)
  45. {
  46. AndroidJavaObject inputStream = new AndroidJavaObject("java.io.ByteArrayInputStream", data);
  47. return m_NativeObject.Call<AndroidJavaObject>("insertImage", inputStream, displayName, folderName, "image/png");
  48. }
  49. public AndroidJavaObject InsertVideo(string originFilePath, string displayName, string folderName)
  50. {
  51. return m_NativeObject.Call<AndroidJavaObject>("insertVideo", originFilePath, displayName, folderName);
  52. }
  53. }
  54. }