How do I cast an argument to a class in Rubymotion Android?

41 views Asked by At

Handling file uploads I need to pass an array of uri's to a android.webkit.ValueCallback

def handleResult(intent)
  uri = Android::Net::Uri.parse(intent.getDataString)
  file_path_callback.onReceiveValue([uri])
end

The problem is that onReceiveValue expects an array of android.net.Uri and the following error is raised:

Java exception raised: java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.net.Uri[]

How do I cast the array to the proper types?

1

There are 1 answers

0
user11013897 On

I fixed it by adding a Util class and implementing a class method in java.

In util.rb

class Util
end

In util.java

// ValueCallback.onReceiveValue only accepts an array of Uris
// This isn't supported by Rubymotion so we create a special method to allow
// sending arrays of Uris
public static void onReceiveValue(
    final android.webkit.ValueCallback callback,
    final android.net.Uri uri) {
    android.net.Uri[] urlList = new android.net.Uri[] { uri };
    callback.onReceiveValue(urlList);
}

Then pass the callback to the Util method:

def handleResult(intent)
  uri = Android::Net::Uri.parse(intent.getDataString)
  Util.onReceiveValue(file_path_callback, uri)
end