0

i am unable to pass the image using share intent.The app runs but when i try to send image to a friend in whatsApp it says "Sharing failed, please try again" and on sharing with gmail it says "Cannot attach empty file" .I have also set permission in Manifests. I guess there is some issue in creating file or maybe the path.

please Check the below code:

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            Bitmap icon = BitmapFactory.decodeResource(getResources(), img);
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            sharingIntent.setType("image/*");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
             File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + "temporary_file.jpg");
           // File f1=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            //File f = new File(Environment.getExternalStorageDirectory() + "/save_picture", "temporary_file.jpg");

            try {
              f.createNewFile();
               // f.mkdir();
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();

            }
           // sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + Environment.getExternalStorageDirectory()));
             sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content:///sdcard/temporary_file.jpg"));
            startActivity(Intent.createChooser(sharingIntent, "Share via: "));


        }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Navin
  • 3
  • 6

2 Answers2

1

I think the URI is malformed. Try this:

Uri.fromFile(file);

instead of

Uri.parse("content:///sdcard/temporary_file.jpg"); // Here probably file path is wrong

Using Uri.fromFile you ensures that the file URI is correct.

If your targetSdkVersion is 24+ you need to use content:// instead of file://. To migrate, checkout this question.

android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

Marc Estrada
  • 1,657
  • 10
  • 20
  • Error : android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/temporary_file.jpg exposed beyond app through ClipData.Item.getUri() – Navin May 15 '18 at 20:59
  • Ok, you are targeting 24+. Take a look to my answer, I've just edited it with a solution. – Marc Estrada May 15 '18 at 21:12
  • But not using the right way. Your Uri path is wrong – Marc Estrada May 15 '18 at 21:43
  • i tried the file provider method and still while sharing image to whatsApp there is a toast "File format is not supported" could u edit my code what Uri path is correct ? – Navin May 15 '18 at 21:46
0

For android 6 and higher, adding permissions in manifest is not enough, you have to request for permission at the runtime. For example, for external storage permission, you have to do this :

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    // permission granted, you can write external storage
}

If is not granted, you have to request the permission :

final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1212;            
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                WRITE_EXTERNAL_STORAGE);

}

For reading external storage, you have to remplace WRITE_EXTERNAL_STORAGE by READ_EXTERNAL_STORAGE

EDIT :

Override onRequestPermissionsResult in your activity :

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
    super.onRequestPermissionsResult(requestCode, permissions, results);
    if(PackageManager.PERMISSION_GRANTED == grantResults[0]){

        //The user accepted the permissions requests, you can do your stuffs here
    }
}

You can find more informations here

E.Abdel
  • 1,992
  • 1
  • 13
  • 24
  • what is the API level of the testing device? – E.Abdel May 15 '18 at 20:17
  • getting this errors: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.apple.wallpapershd, PID: 6274 java.lang.NumberFormatException: For input string: "android.permission.WRITE_EXTERNAL_STORAGE" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at com.example.apple.wallpapershd.WallpaperFullScreen$2.onClick(WallpaperFullScreen.java:94) – Navin May 15 '18 at 20:34
  • the testing device is of API 26 – Navin May 15 '18 at 20:36