2017-02-13 53 views
0

我試圖用如下意圖發送圖像:分享失敗的意圖

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mArray[position])); 
shareIntent.setType("image/png"); 
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(shareIntent); 

編輯:FILE圖像陣列

private void createDrawbleArray(File fileDir) { 

    mArray = new File[Constants.TOTAL_GRID_ICONS]; 
    TypedArray tArray = getResources().obtainTypedArray(R.array._images); 

    for (int i = 0; i < Constants.TOTAL_GRID_ICONS; i++) { 
     mArray[i] = getFileForResource(this, tArray.getResourceId(i, -1), fileDir, "a" + i + ".png"); 
    } 
    tArray.recycle(); 

} 

編輯:PERMISSIONS

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 

但不幸的是它給我分享失敗的敬酒。 可能是什麼問題?

謝謝。

+0

你能分享你的圖片文件位置嗎? –

+0

其陣列我創建爲...編輯... –

+0

/data/user/0/com.example.android.supportv13.sampleime/files/images/a2.png –

回答

1

由於您的圖片文件位置在您的項目中,因此無法與外部應用程序共享圖片。

這裏是共享

  1. 創建文件提供

  2. 店在外部目錄中的圖像文件幾種方法。

檢查這些鏈接來創建文件提供

  1. https://developer.android.com/training/secure-file-sharing/setup-sharing.html

  2. https://developer.android.com/reference/android/support/v4/content/FileProvider.html

  3. http://www.blogc.at/2014/03/23/share-private-files-with-other-apps-fileprovider/

+0

我正在使用CommitContent API的InputMethodService。 –

+0

我可以成功分享「文字」。但是,問題與形象。 –

+0

你好,先生,謝謝你寶貴的回答。 B –

2

最後,我得到了解決:

Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType(getResources().getString(R.string.strIntentType)); 
    Uri uri = FileProvider.getUriForFile(mContext, getResources().getString(R.string.strFileProviderPackage), mArrayIcons[position]); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(shareIntent); 

用於FileProvider這樣做,它的工作原理就像一個魅力.. !!! yeppii。

+0

嘿,還有一個小問題,由於我使用的是Transperent PNG圖像,因此與WhatsApp共享圖像會伴隨黑色背景。我希望它帶有白色背景。任何想法 ? –