如何与Messenger和com.android.mms共享图像?

时间:2015-04-21 20:25:52

标签: android facebook-messenger

我正在尝试与其他应用分享图片。 从文档中,我了解到我必须创建一个ContentProvider以确保从应用程序外部访问我的资源。它适用于大多数应用程序,但Facebook Messenger和消息(com.android.mms)。我有以下错误: FB Messenger:“抱歉,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;
}

有没有人有关于这个问题的想法或经验?

1 个答案:

答案 0 :(得分: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并使用此代码。

相关问题