Android Intent problem

Hey guys,
I’m having problem on scanning media, after capture screenshot from unity with Vuforia QCAR.
I manage to capture and move the file to a folder, but unable to make it appear in gallery.

I’m not familiar with NDK or programming outside Unity.
I have two (2) javascript (.js),

Screenshot.js (Assets/Scripts)
RescanMedia.js (Assets/Plugins/Android)

So my Screenshot does the usual stuff,

Application.CaptureScreenshot(“file.png”);
check for file existent on Application.persistentDataPath
move to a folder (mnt/sdcard/Pictures/ABC)
RescanMedia.ScanNow(“file://mnt/sdcard/Pictures/ABC/file.png”);

This is the RescanMedia.js

#pragma strict

#if UNITY_ANDROID
static function ScanNow(uriString : String) {
print("here1");
var uriClass = AndroidJavaClass("android.net.Uri");

print("here2");
var uri = uriClass.CallStatic.<AndroidJavaObject>("parse", uriString);

print("here3");
var intent = AndroidJavaObject("android.intent.action", "android.intent.action.media_mounted", uri);

print("here4");
var unityPlayerClass = AndroidJavaClass("com.qualcomm.QCARUnityPlayer.QCARPlayerActivity");

print("here5");
var currentActivity = unityPlayerClass.GetStatic.<AndroidJavaObject>("currentActivity");

print("here6");
currentActivity.Call("sendBroadcast", intent);
}
#endif

So basically, I’m trying to replicated this line of code to rescan my file.png

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

And I get this error on logcat

.
..
...
here3
JNI ERROR (app bug): accessed stale weak global reference 0x13 (index 4 in a table of size 0)
VM aborting
...
..
.

I still using the Android manifest provided by QCAR. Do I need to make any changes in there?
If anyone have know how to make the picture appear in gallery, I would appreciate a tentative guide.

cheers,
-SIM-

1 Like

android.intent.action” should probably be “android.content.Intent” since you are trying to replicate new Intent().

Is ok for now…
I get things work fine.

Debug.Log("**create activity instance");
AndroidJavaClass classPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject objActivity = classPlayer.GetStatic<AndroidJavaObject>("currentActivity");
		
Debug.Log("**create Uri class");
AndroidJavaClass classUri = new AndroidJavaClass("android.net.Uri");
		
Debug.Log("**create Intent object");
AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2]{"android.intent.action.MEDIA_MOUNTED", classUri.CallStatic<AndroidJavaObject>("parse", "file:///mnt/sdcard/TestFolder/SubFolder/" + filename)});
		
Debug.Log("**call sendBroadcast");
objActivity.Call ("sendBroadcast", objIntent);
1 Like

does any one can convert this code to javascript ?

This works great! Thanks!