2015-04-21 14 views
0

我想分享一個圖像到其他應用程序。 從文檔中,我知道我必須創建一個ContentProvider以確保從應用程序外部訪問我的資源。它適用於大多數應用程序,但Facebook Messenger和消息(com.android.mms)。我有以下錯誤: FB使者:「對不起,信使無法處理文件」 com.android.mms:「無法附加文件,不支持」如何使用Messenger和com.android.mms共享圖像?

我在活動中調用共享代碼:

Uri path = Uri.parse("content://com.myauthority/test.png"); 
Intent shareIntent = new Intent(Intent.ACTION_SEND).putExtra(Intent.EXTRA_STREAM, path).setType("image/png"); 
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share))); 

的在我的內容提供商,我只覆蓋中openFile:

@Override 
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 
    String fileName = uri.getLastPathSegment(); 
    String resName = fileName.replaceAll(".png",""); 
    int resId = getContext().getResources().getIdentifier(resName,"drawable",getContext().getPackageName()); 
    File file = new File(getContext().getCacheDir(), fileName); 
    Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), resId); 
    FileOutputStream fileoutputstream = new FileOutputStream(file); 
    boolean flag = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileoutputstream); 
    try { 
     ParcelFileDescriptor parcelfiledescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY | ParcelFileDescriptor.MODE_WORLD_READABLE); 
     return parcelfiledescriptor; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
} 

有沒有人一個想法或經驗關於這個問題分享?

回答

2

這裏包括我的代碼,我用來從我的應用程序共享數據到Facebook應用程序或Messenger應用程序。

imageView.setDrawingCacheEnabled(true); 
       Bitmap bitmap = imageView.getDrawingCache(); 
       File root = Environment.getExternalStorageDirectory(); 
       final File cachePath = new File(root.getAbsolutePath() 
         + "/DCIM/Camera/Avi.jpg"); 
       try { 
        cachePath.createNewFile(); 
        FileOutputStream ostream = new FileOutputStream(
          cachePath); 
        bitmap.compress(CompressFormat.JPEG, 100, ostream); 
        ostream.flush(); 
        ostream.close(); 
        new Handler().postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          Intent intent = new Intent(); 
          intent.setType("image/*"); 
          intent.setAction(Intent.ACTION_SEND); 
          intent.putExtra(
            Intent.EXTRA_STREAM, 
            Uri.fromFile(new File(cachePath 
              .getAbsolutePath()))); 
          Log.e("Path for sending ", 
            ""+Uri.fromFile(new File(cachePath 
              .getAbsolutePath()))); 
          mContext.startActivity(intent); 
         } 
        }, 3000); 

只是提供您的圖像uri並使用此代碼。

+0

謝謝。你爲什麼推遲分享? – znat

+1

只是爲了安全起見,如果某些手機需要很長時間才能處理數據,則需要花時間延遲。您可以更改該值。 –

相關問題