Android:分享到 Facebook Messenger 错误

Android: Share to Facebook Messenger Error

我已经尝试修复这个错误一个星期了,但仍然找不到问题所在。登录正常,但由于某种原因共享到 Messenger 崩溃。我真的不明白问题是什么。基本上,我将文本转语音保存到我的设备上,然后我尝试分享它。文件保存到设备(我检查过),但 Messenger 不共享。

这是我分享的代码:

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);

}

和我的 logcat:

 java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View.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.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.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)

您需要阅读您的 logcat 并尝试理解它告诉您的内容。

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

好的...为什么?

Caused by: java.lang.reflect.InvocationTargetException

这只是意味着它无法调用目标方法 - 与第一条消息非常相似。仍然..为什么?

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

IllegalArgumentException 表示传递给方法的参数有问题。在哪里?在您的代码中找到第一个位置:

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)
 ...

所以这告诉你 PersonalSettings.java 中的第 205 行,参数有问题。问题是在调用 build() 时使用空 URI 方案。

我猜测,contentUri 参数可能是问题所在。 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

对 URI 的一些研究(比如来自 this question 的帮助)让我们看到 uriString 需要有一个像 http://file:// 这样的方案字符串的开头。

在您的情况下,您调用 Uri.parse 时使用的是普通文件名 而没有方案 ,导致在 logcat 中向您显示错误。

添加适当的方案或使用 Uri class 中的辅助函数应该有助于解决问题:

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