简体   繁体   English

Unity将图像保存到图库android / ios

[英]Unity save image to gallery android / ios

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. 如果我在SD卡上查找,它就在那里。 But in the gallery it is not showing up. 但是在画廊里却没有露面。 So I tried this after File.move, to refresh the gallery. 所以我在File.move之后尝试了这个,刷新了库。

//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. 我正在使用最新的Unity版本5.3,它应该适用于所有Android和ios设备。 Any ideas how to get this working and show up anywhere in the galler? 任何想法如何使这个工作,并出现在画廊的任何地方?

Thank you! 谢谢!

    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. 只需将mypath替换为myScreenshotLocation即可。 Note this will only works with android KitKat 4.4 above. 请注意,这只适用于上面的Android KitKat 4.4。

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?). 你可能需要调整你的“刷新Android手机图片库”代码(你在Android 4.4+上测试,对吧?)。

Your problems looks like you need to give WRITE_PERMISSIOS_EXTERNAL in the Android manifest. 您的问题看起来需要在Android清单中提供WRITE_PERMISSIOS_EXTERNAL。

There is a plugin for handling the Native Gallery in Unity called "Native Gallery Assist" which will help you which such funtionality. 有一个插件用于处理Unity中的Native Gallery,称为“Native Gallery Assist”,它将帮助您实现这样的功能。 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 ) 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  -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM