Search Unity

Android Intent problem

Discussion in 'Android' started by shinichi88, Nov 30, 2012.

  1. shinichi88

    shinichi88

    Joined:
    May 4, 2011
    Posts:
    80
    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
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. #if UNITY_ANDROID
    5. static function ScanNow(uriString : String) {
    6. print("here1");
    7. var uriClass = AndroidJavaClass("android.net.Uri");
    8.  
    9. print("here2");
    10. var uri = uriClass.CallStatic.<AndroidJavaObject>("parse", uriString);
    11.  
    12. print("here3");
    13. var intent = AndroidJavaObject("android.intent.action", "android.intent.action.media_mounted", uri);
    14.  
    15. print("here4");
    16. var unityPlayerClass = AndroidJavaClass("com.qualcomm.QCARUnityPlayer.QCARPlayerActivity");
    17.  
    18. print("here5");
    19. var currentActivity = unityPlayerClass.GetStatic.<AndroidJavaObject>("currentActivity");
    20.  
    21. print("here6");
    22. currentActivity.Call("sendBroadcast", intent);
    23. }
    24. #endif
    25.  
    So basically, I'm trying to replicated this line of code to rescan my file.png
    Code (csharp):
    1.  
    2. sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    3.  
    And I get this error on logcat
    Code (csharp):
    1.  
    2. .
    3. ..
    4. ...
    5. here3
    6. JNI ERROR (app bug): accessed stale weak global reference 0x13 (index 4 in a table of size 0)
    7. VM aborting
    8. ...
    9. ..
    10. .
    11.  
    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-
     
    Last edited: Nov 30, 2012
    Kurius likes this.
  2. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    "android.intent.action" should probably be "android.content.Intent" since you are trying to replicate new Intent().
     
  3. shinichi88

    shinichi88

    Joined:
    May 4, 2011
    Posts:
    80
    Is ok for now..
    I get things work fine.

    Code (csharp):
    1.  
    2. Debug.Log("**create activity instance");
    3. AndroidJavaClass classPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    4. AndroidJavaObject objActivity = classPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    5.        
    6. Debug.Log("**create Uri class");
    7. AndroidJavaClass classUri = new AndroidJavaClass("android.net.Uri");
    8.        
    9. Debug.Log("**create Intent object");
    10. 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)});
    11.        
    12. Debug.Log("**call sendBroadcast");
    13. objActivity.Call ("sendBroadcast", objIntent);
    14.  
     
    Kurius likes this.
  4. sorushe-mehre

    sorushe-mehre

    Joined:
    Aug 12, 2013
    Posts:
    8
    does any one can convert this code to javascript ?
     
  5. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    This works great! Thanks!