2

I want to share multiple image from application package directory. My files locate in following path as like. /data/data/com.example. Now i can get images path. But not attach any images to sharing application. as like whats app, Message and Facebook etc. I have implemented permition in manifest file. See below my try.

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(saveList.get(0).toString())));
            startActivity(Intent.createChooser(intent, "Share"));
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND_MULTIPLE);
            intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
            intent.setType("image/*"); /* This example is sharing jpeg images. */
            ArrayList<Uri> files = new ArrayList<Uri>();
            for(String path : saveList /* List of the files you want to send */) {
                File file = new File(path);
                Uri uri = Uri.fromFile(file);
                files.add(uri);
            }

            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
            startActivity(intent);  

Implemented permission is below.

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
Ravi Vaghela
  • 3,420
  • 2
  • 23
  • 51

2 Answers2

1

It is compatible with scoped storage. So don't need any storage permission.

fun shareFile(){
    var intentBuilder: ShareCompat.IntentBuilder
    intentBuilder = ShareCompat.IntentBuilder.from(this)
        .setType("image/jpg")
    intentBuilder.addStream(FileProvider.getUriForFile(this.baseContext!!
        , getString(R.string.package), File(path)))
    val intent = intentBuilder.intent.setAction(Intent.ACTION_SEND).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    startActivity(Intent.createChooser(intent, "Send to "))
}

In Manifest

 <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

Create a xml file

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <files-path path="/" name="allfiles" />
  <external-path path="/" name="externalfiles" />
  <root-path name="external_files" path="/storage/" />
</paths>
venkatesh
  • 319
  • 2
  • 10
0

First, Uri.fromFile() will not work in your case, because third-party apps have no access to your internal storage.

Second, using a file: Uri, such as from Uri.fromFile(), appears as though it will be banned in the next version of Android.

Instead, you need to use some sort of ContentProvider, such as FileProvider, to make the files available to third-party apps. This is covered in the documentation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @RaviVaghela: [This sample app](https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/V4FileProvider) shows using `FileProvider`, albeit for `ACTION_VIEW`, not `ACTION_SEND`. – CommonsWare Mar 23 '16 at 11:08
  • my question is i want to share image from application package folder like `/data/data/com.example/image/image1.jpg` this type. I am store image in package folder i need it for share image i can show in imageview but not share. @CommonsWare – Ravi Vaghela Mar 23 '16 at 11:23
  • @RaviVaghela: That is not a standard location; `FileProvider` will not support it. You will need to write your own streaming `ContentProvider`, or switch to storing your files in a more conventional spot (e.g., `getFilesDir()`) that `FileProvider` supports. – CommonsWare Mar 23 '16 at 11:49
  • yes that for i have to copy data in external storege and share it – Ravi Vaghela Mar 23 '16 at 11:51
  • @RaviVaghela: That will not help you starting with the next version of Android, as [Google starts banning `file:` `Uri` values](https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html). – CommonsWare Mar 23 '16 at 11:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107129/discussion-between-ravi-vaghela-and-commonsware). – Ravi Vaghela Mar 23 '16 at 11:58