Search code examples
androidcordovaapkphonegap

How to share my own app apk to other using cordova


I want to share my own apk file to other, in cordova application. I had tried many plugin, but all the plugin are used to just share the app name and some description only.

https://www.npmjs.com/package/cordova-plugin-share https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin

So I have decided to create my own plugin and I have search share apk code for java, I got the following code, and it is working fine when I call that function from MainActivity.java

   private void shareApplication() {
    ApplicationInfo app = 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(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() + "/" + 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
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
        startActivity(Intent.createChooser(intent, "Share app via"));

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

But I want to call that function(shareApplication()) from cordova extended java file.

public class AppVersion extends CordovaPlugin {
  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {
      if (action.equals("shareApk")) {
            MainActivity cc=new MainActivity();
            cc.shareApplication();
            MyClass myClass = new MyClass(c);
      }
      return false;
    } catch (NameNotFoundException e) {
      callbackContext.success("N/A");
      return true;
    }
}

But when I call the function from cordova extended class, it show following error. enter image description here


Solution

  • I have found the plugin in the following URLs, this can generate your app apk file and finally popup the available sharing capable applications. Then you can choose the application and share it.

    https://github.com/merbin2012/cordova-plugin-codeplay-share-own-apk https://www.npmjs.com/package/cordova-plugin-codeplay-share-own-apk

    cordova plugin add cordova-plugin-codeplay-share-own-apk