0

I have checked the uri, the file saved is a bitmap and its returning file. But the image which shows during sharing is not valid. Gmail says it could not attach the attachment, messages app could not load the image.

Text sharing is working fine.

I am new to Android and is having problem sharing image through share intent. I have googled alot, tried various things but still could not find the solution.

public static void shareWallpaper(Context context, int wallpaperResourceId) {

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), wallpaperResourceId);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/wallpaper" + String.valueOf(wallpaperResourceId) + ".jpg";
    OutputStream out = null;
    File file = new File(path);
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getPath()));
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    context.startActivity(Intent.createChooser(shareIntent, "برنامه مورد نظر خود را انتخاب کنید"));


}
mohamad
  • 1
  • 1
  • 2
    `Uri.fromFile()` will not work on Android 7.0+ due to the ban on `file` as a `Uri` scheme. Use `FileProvider`. Also, use a concrete MIME type. You know that your image is a JPEG, so use `image/jpeg` as the MIME type, not `image/*`. – CommonsWare Jul 15 '19 at 18:44
  • ok!how can i implement FileProvider ? – mohamad Jul 15 '19 at 22:01
  • `FileProvider` has [JavaDocs](https://developer.android.com/reference/androidx/core/content/FileProvider) along with [a few pages of programming guides](https://developer.android.com/reference/androidx/core/content/FileProvider). [This sample project](https://gitlab.com/commonsguy/cw-jetpack-java/tree/v0.4/PdfProvider) from [this book](https://commonsware.com/Jetpack) uses `FileProvider`, though in my case it is for `ACTION_VIEW` of a PDF file. – CommonsWare Jul 15 '19 at 22:04

0 Answers0