5

I am trying to save my screenshot to the image gallery of my device. It works fine but it won't show up in the gallery.

This is what I do

selfie= new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
selfie.ReadPixels(new Rect(0, 0,Screen.width, Screen.height), 0, 0,false);

selfie.Apply ();
byte[] bytes = selfie.EncodeToPNG();
string filename = "Screenshot.png";

fileLocation = Path.Combine( Application.persistentDataPath, filename );
File.WriteAllBytes(fileLocation, bytes );

string myFolderLocation = "/mnt/sdcard/DCIM/Camera/";
myScreenshotLocation = myFolderLocation + filename;

System.IO.File.Move(fileLocation, myScreenshotLocation);

It saves the image on the right place. If I look it up on the sdcard, it is there. But in the gallery it is not showing up. So I tried this after File.move, to refresh the gallery.

//REFRESHING THE ANDROID PHONE PHOTO GALLERY IS BEGUN
    AndroidJavaClass classPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject objActivity = classPlayer.GetStatic<AndroidJavaObject>("currentActivity");        
    AndroidJavaClass classUri = new AndroidJavaClass("android.net.Uri");        
    AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2]{"android.intent.action.MEDIA_MOUNTED", classUri.CallStatic<AndroidJavaObject>("parse", "file://" + myScreenshotLocation)});        
    objActivity.Call ("sendBroadcast", objIntent);
//REFRESHING THE ANDROID PHONE PHOTO GALLERY IS COMPLETE

But this is doing nothing. I am working with the latest Unity version 5.3 and it should work on all android and ios devices. Any ideas how to get this working and show up anywhere in the galler?

Thank you!

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Jenny
  • 469
  • 2
  • 11
  • 25

3 Answers3

2
    AndroidJavaClass classPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject objActivity = classPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass classMedia = new AndroidJavaClass("android.media.MediaScannerConnection");
    classMedia.CallStatic("scanFile", new object[4] { objActivity,
        new string[] { _path },
        new string[] { "image/png" },
        null  });

Try the code above, since you have already saved the image. Just replace the _path with your myScreenshotLocation. Note this will only works with android KitKat 4.4 above.

Sam Lee
  • 21
  • 4
0

Have a look at the most upvoted answer here.

You probably need to adapt your "REFRESHING THE ANDROID PHONE PHOTO GALLERY" code (you're testing on Android 4.4+, right?).

Community
  • 1
  • 1
-1

Your problems looks like you need to give WRITE_PERMISSIOS_EXTERNAL in the Android manifest.

There is a plugin for handling the Native Gallery in Unity called "Native Gallery Assist" which will help you which such funtionality. We used it for the users in a social app/game for the users to be able to take images of themselfes update and share it with each other. (https://www.assetstore.unity3d.com/en/#!/content/60922)

We essentally had the same problem before we added the Write permissions.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--- Thomas: Needed by Native Gallery Assist to be able to save images to the album  -->
Chris
  • 498
  • 5
  • 16