2016-12-03 25 views
0

您好evrybody我正在tryng,分享使者的形象,但我不知道爲什麼我的代碼doesen't的工作,我已經按照官方指導,https://developers.facebook.com/docs/messenger/android 有人可以告訴我爲什麼doese'nt工作?的Android,如何共享圖像文件與信使

public void sendMessage(){ 
    Bitmap adv= takePic(HomeActivity.livelloCurrent.getNumeroLivello()); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "temporary_file.jpg"); 
    try { 
     f.createNewFile(); 
     new FileOutputStream(f).write(bytes.toByteArray()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    String mimeType = "image/jpeg"; 
    Intent sendIntent = new Intent(); 
    sendIntent.setType(mimeType); 
    sendIntent.setAction(Intent.ACTION_SEND); 
    sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg")); 
    sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->."); 
    sendIntent.setPackage("com.facebook.orca"); 
    try { 
     startActivity(sendIntent); 
    } 
    catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(getApplicationContext(),"Please Install Facebook Messenger", Toast.LENGTH_LONG).show(); 
    } 
    /** //withSDK-->// ShareToMessengerParams shareToMessengerParams = ShareToMessengerParams.newBuilder(ContentUri, mimeType).build(); 
    MessengerUtils.shareToMessenger(this, REQUEST_CODE_SHARE_TO_MESSENGER, shareToMessengerParams);**/ 
} 

我確定文件創建工作導致我測試了它。在測試中,我從Messenger中得到以下錯誤「對不起,Messenger無法處理文件」。我如何解決?

+0

呼叫'平齊()','getFD()同步()','和關閉後()'上'FileOutputStream'。 '寫()'。有一天,使用'FileProvider'而不是'Uri.fromFile()',因爲'fromFile()'在你的'targetSdkVersion'設置爲24或更高的時候不適用於Android 7.0+設備。請注意,並非所有應用都支持*'EXTRA_TEXT'和'EXTRA_STREAM'在同一個「ACTION_SEND」'Intent'上。並擺脫'setPackage()',所以你可以[分享用戶想要的](https://commonsware.com/blog/2011/06/28/share-where-the-user-wants.html)。 – CommonsWare

+0

在write()之後,我在'FileOutputStream'上調用了'flush()','getFD()。sync()'和'close()',但仍然不起作用,我也刪除了'EXTRA_TEXT' – Rachid

回答

0

替換:

Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg") 

與:

Uri.fromFile(f) 
+0

我在日誌控制檯上有這個致命異常 'android.os.FileUriExposedException:file:///storage/emulated/0/temporary_file.jpg通過ClipData.Item.getUri()暴露超出應用程序' – Rachid

+0

@Rachid:這就是我在第一條評論中提到的問題。您無法在Android 7.0+上使用'Uri.fromFile()',其targetSdkVersion爲24或更高。一種選擇是進入'build.gradle'文件並將'targetSdkVersion'降低到23.另一種方法是將所有這些修改爲使用'FileProvider',這樣你就可以使用它而不是試圖傳遞你從'Uri.fromFile()'得到的'Uri'。 – CommonsWare

+0

三江源洙多有解決我的問題:) :)使用FileProvider – Rachid

相關問題