unity刷新android相册,如何使用Unity刷新android Gallery?

博主在经历多次尝试和搜索后,找到了一种在Android 6.0.1上成功刷新图库的方法。通过使用Android Java类和方法,如UnityPlayer、MediaScannerConnection等,实现了对图片路径的扫描,从而更新图库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tons of people asking this, and I've been through the first few pages of google search results and tried every solution I've found - None seem to work.

Seems to be because things have changed with different versions etc...

I have bought an asset from Unity Asset store, but the asset doesn't do this itself either. (I have already contacted the asset creator, but not heard anything back about this yet).

So... What is the current way to refresh the gallery? What works in the most recent versions of Android?

Solutions1

OK, so after a lot more head scratching, I found a solution.

using (AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))

using (AndroidJavaObject joActivity = jcUnityPlayer.GetStatic("currentActivity"))

using (AndroidJavaObject joContext = joActivity.Call("getApplicationContext"))

using (AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))

using (AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"))

using (AndroidJavaObject joExDir = jcEnvironment.CallStatic("getExternalStorageDirectory"))

{

jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { Path }, null, null);

}

Path should be equal to the absolute path of your image. Insert more path's into the string[] as required. Have tested this on Android 6.0.1 and works great!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值