Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Guy-Corbett · Jan 28, 2016 at 01:27 PM · androidjavaexceptionnativeandroidjavaobject

NoSuchMethodError calling android method

Hi all,

I am trying to use native android functions to insert an image I've saved to disk to the device's gallery. This should be just case of one simple call to this insertImage function but I keep getting an exception whenever I try to make the call. Here's my code;

 AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 AndroidJavaObject currentActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
 AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject>("getContentResolver");
 AndroidJavaClass media = new AndroidJavaClass("android.provider.MediaStore$Images$Media");
 media.CallStatic<string>("insertImage ", contentResolver, destination, "Affermational Image", "AFFIRMATIVE");

And here's the exception which is getting thrown on the last line;

"java.lang.NoSuchMethodError: no static method \"Landroid/provider/MediaStore$Images$Media;.insertImage (Landroid.app.ContextImpl$ApplicationContentResolver;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;\""

Obviously it's saying the method doesn't exist but does anyone know why?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by saschandroid · Jan 29, 2016 at 07:29 AM

I don't know if this could be the problem, but you have a whitespace in your java function string:

 media.CallStatic<string>("insertImage ",
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Guy-Corbett · Jan 29, 2016 at 07:45 AM 0
Share

Well Duh. Don't I feel stupid. That fixed it. Thanks!

avatar image
1

Answer by Bunny83 · Jan 29, 2016 at 01:44 AM

Well, the problem is how Unity's wrapper classes are determine the method signatures. Unity simply reads the .NET type of the passed parameters and converts them to the proper Java type. This usually works perfect for most primitive types. However it's a problem with reference types.

You used "getContentResolver" which returned some implementation of the abstract android.content.ContentResolver class. The signature of insertImage has this abstract class as type argument. However you actually pass a concrete implementation of that abstract class.

So Unity detemines the type of the passed class which is Landroid.app.ContextImpl$ApplicationContentResolver but the required type is Landroid.content.ContentResolver. That's why it can't automatically find the method you want to call. That means you can't use the wrapper class AndroidJavaObject to call that method in this case and have to do it "manually". You need to use the methods from the AndroidJNI class (which are also used by AndroidJavaObject and AndroidJavaClass internally).

I'm not that fit with JNI, but you basically need:

  • GetMethodID and pass the class reference (you get that from your AndroidJavaObject with GetRawClass), the method name and the method signature. The signature should be "(Landroid.content.ContentResolver;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;. For reference here's the oracle documentation of the JNI method signature syntax

  • Next you need to create your parameter array for calling that method. You can use the methods from AndroidJNIHelper, specifically CreateJNIArgArray and when done don't forget DeleteJNIArgArray. Keep in mind that for java object references you need the actual object reference and not the wrapper class, so use GetRawObject.

  • Once you have the methodID and the parameter array ready, you have to pick the right "call" method. Since you should get a string back, you probably want to use AndroidJNI.CallStaticStringMethod

Keep in mind that you have to clean up almost everything manually. So be careful what you allocate and make sure you either destroy / delete the object you created or make sure Dispose() is called on it.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 29, 2016 at 01:54 AM 0
Share

I just checked it. CreateJNIArgArray already does a lot for you. It creates native java strings for your .NET string parameters and it also calls "GetRawObject" if you pass a AndroidJavaObject to that method.

avatar image Guy-Corbett Bunny83 · Jan 29, 2016 at 07:53 AM 0
Share

Thanks for going to the trouble of explaining that. I wasn't aware of the AndroidJNI options. However it seems in this case that's not needed. As @saschandroid pointed out I just had some extra whitespace causing problems. I removed it and it all seemed to work fine.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to translate ArrayList to C# using AndroidJavaClass? 1 Answer

Can't access android mediastore 1 Answer

Using AndroidJavaObject properly 1 Answer

AndroidJavaObject.Call fails with 'method not found' 1 Answer

AndroidJavaClass. Call returns null ptr. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges