0

I am trying to share an image to other apps. From the doc, I understand I have to create a ContentProvider to ensure access to my resource from outside the app. It works with most apps but Facebook Messenger and Messages (com.android.mms). I have the following errors: FB Messenger: "Sorry, messenger was unable to process the file" com.android.mms: "Unable to attach. File not supported"

The code I call in the activity to share:

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)));

The in my content provider I only override 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;
}

Has anyone an idea or experience to share regarding this issue?

znat
  • 13,144
  • 17
  • 71
  • 106

1 Answers1

2

Here am Including my code which i used to share data from my app to facebook app or messenger app.

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);

just provide your image uri and use this code.

Avinash Ranjan
  • 611
  • 5
  • 14
  • 1
    Just giving time to delay for safety that if some phone take bit long time to process the data.You can change that value. – Avinash Ranjan Apr 21 '15 at 20:38