2015-04-21 22 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

只是为了安全起见,如果某些手机需要很长时间才能处理数据,则需要花时间延迟。您可以更改该值。 –