Android: Share to Facebook Messenger Error

2019-02-27 03:28发布

问题:

I have been trying to fix this error for a week now, but still cannot find the problem. The LogIn is working, but for some reason the share to Messenger Crashes. I cannot really understand what the problem is. Basically I save a text to speech to my device and then I attempt to share it. The file saves to the device (I checked), but messenger does not share.

Here is my code for sharing:

public void shareFile(View view) {
    String sayWhat = "This is what you sound like when you write to me, thanks to the app Messenger Say it! Available now at Google Play";
    HashMap hashmap = new HashMap();
    hashmap.put("utteranceId", sayWhat);
    String location = new StringBuilder(Environment.getExternalStorageDirectory().toString()).append("/").append(name).append(".mp3").toString();


    speed = ((float)speedS.getProgress()/100F)*2.0F;
    pitch=((float)pitchS.getProgress()/100F)*2.0F;
    ttsread.setPitch(pitch);
    ttsread.setSpeechRate(speed);
    ttsread.synthesizeToFile(sayWhat, hashmap, location);

    String mimeType = "audio/mpeg";


    Uri contentUri = Uri.parse(location);
    text.setText(contentUri.toString());
    long futuretime = System.currentTimeMillis() + 1000;
    while (System.currentTimeMillis() < futuretime) {
        synchronized (this) {
            try {
                wait(futuretime - System.currentTimeMillis());
            } catch (Exception e) {
            }
        }
    }
   ShareToMessengerParams params = ShareToMessengerParams.newBuilder(contentUri, "audio/mpeg")
            .setMetaData("com.facebook.sdk.ApplicationId")
            .build();
    MessengerUtils.shareToMessenger(this, 1, params);

    //MessengerUtils.finishShareToMessenger(activity,params);

}

and my logcat:

 java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3969)
            at android.view.View.performClick(View.java:4637)
            at android.view.View$PerformClick.run(View.java:19422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5586)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4637)
            at android.view.View$PerformClick.run(View.java:19422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5586)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: Unsupported URI scheme: null
            at com.facebook.messenger.ShareToMessengerParams.<init>(ShareToMessengerParams.java:106)
            at com.facebook.messenger.ShareToMessengerParamsBuilder.build(ShareToMessengerParamsBuilder.java:120)
            at com.inc.nicky.messengersayit.PersonalSettings.shareFile(PersonalSettings.java:205)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4637)
            at android.view.View$PerformClick.run(View.java:19422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5586)

回答1:

You need to read your logcat and try and understand what it is telling you.

java.lang.IllegalStateException: Could not execute method of the activity

OK...why?

Caused by: java.lang.reflect.InvocationTargetException

This just means it failed to invoke the target method - so pretty much the same as the first message. Still..why?

Caused by: java.lang.IllegalArgumentException: Unsupported URI scheme: null

IllegalArgumentException means something is wrong with an argument passed to a method. Where? Find the first location in your code:

at com.facebook.messenger.ShareToMessengerParams.<init>(ShareToMessengerParams.java:106)
            at com.facebook.messenger.ShareToMessengerParamsBuilder.build(ShareToMessengerParamsBuilder.java:120)
            at com.inc.nicky.messengersayit.PersonalSettings.shareFile(PersonalSettings.java:205)
 ...

So this tells you that on line 205 in PersonalSettings.java, there is a problem with an argument. And the problem is to do with a null URI scheme when build() is called.

At a guess, I'd say the contentUri parameter could be the problem. This value is being set earlier by Uri contentUri = Uri.parse(location);. Uri.parse(), according to the docs:

Creates a Uri which parses the given encoded URI string.

Parameters: uriString: an RFC 2396-compliant, encoded URI

A little bit of research into URIs (like help from this question) leads us to see that uriString needs to have a scheme like http:// or file:// at the start of the string.

In your case, you are calling Uri.parse with a plain filename without a scheme, resulting in the error presented to you in logcat.

Adding an appropriate scheme or using a helper function from the Uri class should help to fix the problem:

 Uri contentUri = Uri.fromFile(new File(location));