2

I've tried to share the app's apk file through intent.The provider in my manifest file:

`<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="package name"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/mypaths" />

and the mypaths file is:

<paths>
    <external-path name="apk_folder"/>
</paths>

I set intent and the File path as below:

String packageName = getContext().getPackageName();
                PackageManager pm = getContext().getPackageManager();
        String apk = null;
                try {
                    ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
                     apk = ai.publicSourceDir;

                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                } 
                File apkFile = new File(apk);

                Uri uri = FileProvider.getUriForFile(getContext(), "package name", apkFile);

                Intent intent = ShareCompat.IntentBuilder.from(getActivity())
                        .setType("*/*")
                        .setStream(uri)
                        .setChooserTitle("Share via")
                        .createChooserIntent()
                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                startActivity(intent);

I get IllegalArgumentException :

Failed to find configured root that contains /data/app/package name.edu-1/base.apk

Please help me to find out my mistakes.

3 Answers 3

4

change the "package name" in your code to activity.getPackageName() and cahnge path to root-path

or try This codes:

in Manifest

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

in res/xml/file_paths.xml

<paths>
    <root-path
        name="app"
        path="/"/>
</paths>

Code :

public static void sendApkFile(Activity activity) {
    try {

      PackageManager pm = activity.getPackageManager();
      ApplicationInfo ai = pm.getApplicationInfo(activity.getPackageName(), 0);
      File srcFile = new File(ai.publicSourceDir);

      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setType("*/*");
      Uri uri = FileProvider.getUriForFile(context, activity.getPackageName(), srcFile);
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      activity.grantUriPermission(activity.getPackageManager().toString(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
      activity.startActivity(intent);

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

And IF you Want to APK File have any other name , not just base.APK , try this

public static void sendApplication(Activity activity) {
    ApplicationInfo app = activity.getApplicationContext().getApplicationInfo();
    String filePath = app.sourceDir;

    Intent intent = new Intent(Intent.ACTION_SEND);

    // MIME of .apk is "application/vnd.android.package-archive".
    // but Bluetooth does not accept this. Let's use "*/*" instead.
    intent.setType("*/*");

    // Append file and send Intent
    File originalApk = new File(filePath);

    try {
      //Make new directory in new location
      File tempFile = new File(activity.getExternalCacheDir() + "/ExtractedApk");
      //If directory doesn't exists create new
      if (!tempFile.isDirectory()) {
        if (!tempFile.mkdirs()) {
          return;
        }
      }
      //Get application's name and convert to lowercase
      tempFile = new File(tempFile.getPath() + "/" + activity.getString(app.labelRes).replace(" ", "").toLowerCase() + ".apk");
      //If file doesn't exists create new
      if (!tempFile.exists()) {
        if (!tempFile.createNewFile()) {
          return;
        }
      }
      //Copy file to new location
      InputStream in = new FileInputStream(originalApk);
      OutputStream out = new FileOutputStream(tempFile);

      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      in.close();
      out.close();
      System.out.println("File copied.");
      //Open share dialog

      Uri uri = FileProvider.getUriForFile(context, activity.getPackageName(), tempFile);
      intent.putExtra(Intent.EXTRA_STREAM, uri);
      activity.grantUriPermission(activity.getPackageManager().toString(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
      activity.startActivity(intent);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
1
  • Perfect. Thanks @Criss Nov 30, 2021 at 8:49
2

i can solve exact this problem, only use: <?xml version="1.0" encoding="utf-8"?> <resources> <root-path path="data/app/" name="external_files"/> </resources>

0

There is no available root in FileProvider that will handle your designated location. You would need to create your own ContentProvider for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.