-1

to share an image and a text with the other app, I did that:

File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES+"eVendeur");
              if (!path.exists()){
                  path.mkdir();
              }

              File root = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
              File cachePath = new File(root.getAbsolutePath()+"/eVendeur/"+id_produit+".jpg");

    try {
        cachePath.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(cachePath);
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("*/*");
    share.putExtra(Intent.EXTRA_TEXT, "my text");

    if(Build.VERSION.SDK_INT>=24){
                        try{
                            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                            m.invoke(null);
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }

    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
    view.getContext().startActivity(Intent.createChooser(share, "Share:"));

it works but on some devices, im getting :

java.io.IOException: No such file or directory....

what should i change in the code for it to work?

thanks in advance

deslok
  • 51
  • 7
  • You must use Scoped Storage – Ruben Meiring Oct 17 '19 at 11:03
  • Possible duplicate of [Environment.getExternalStorageDirectory() deprecated in API level 29 java](https://stackoverflow.com/questions/57116335/environment-getexternalstoragedirectory-deprecated-in-api-level-29-java) – Ricardo A. Oct 17 '19 at 11:07
  • 2
    `Uri.fromFile()` has been banned (more or less) since Android 7.0, as it triggers a `FileUriExposedException`. "it works except on API 29" -- you will start running into problems before that. Plus, you assume that other apps have access to that location, and there is no requirement for another app to have `READ_EXTERNAL_STORAGE`. And, `EXTRA_STREAM` is supposed to have a `content` `Uri` anyway. Use `FileProvider` to serve your content. – CommonsWare Oct 17 '19 at 11:07
  • @CommonsWare please how to use all this – deslok Oct 17 '19 at 12:06

2 Answers2

1

Use getExternalFilesDir instead of getExternalStoragePublicDirectory

 File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES+"/eVendeur");

 if (!dossier.exists()){
    dossier.mkdir();
 }

 File root = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File cachePath = new
        File(root.getAbsolutePath()+"/eVendeur/"+"id_produit"+".jpg");

For more details visit Docs

Praveen
  • 430
  • 3
  • 11
  • Environment.getExternalStorageDirectory() is deprecated – deslok Oct 17 '19 at 12:01
  • im getting FileUriExposedException : file:///storage/emulated/0/Android/data/com.e_vendeur/files/Pictures/eVendeur/-LqzYHtp3h-1qbHU6PK_.jpg exposed beyond app through ClipData.Item.getUri() – deslok Oct 17 '19 at 12:50
  • i update my question now i m getting java.io.IOException: No such file or directory... – deslok Oct 17 '19 at 22:57
  • Add / in first line in front of eVendeur(File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES+"/eVendeur"); ) Do you have id_produit.jpg in /storage/emulated/0/Android/data/com.example.myapplication/files/Pictures/eVendeur – Praveen Oct 18 '19 at 05:09
0

If you are getting :

FileUriExposedException : file:///storage/emulated/0/Android/data/com.e_vendeur/files/Pictures/eVendeur/-LqzYHtp3h-1qbHU6PK_.jpg exposed beyond app through ClipData.Item.getUri()

then add this before calling intent :

   if(Build.VERSION.SDK_INT>=24){
                            try{
                                Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                                m.invoke(null);
                            }catch(Exception e){
                                e.printStackTrace();
                            }
                        }
Kdon Patel
  • 107
  • 1
  • 15