Mahdi
Mahdi

Reputation: 62

change apk filename when share it from application

i share my app with following code :

ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
startActivity(Intent.createChooser(intent, "Share app via"));

But the file name that shared is "base.apk",how to change it ?

Upvotes: 3

Views: 435

Answers (1)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11642

For sharing the app with apk name change, You can copy the apk file to your sd card using the following method, pass ApplicationItem, expected folder and the new expected name for the apk

public static File copyFile(ApplicationInfo appInfo, File folderName, String apkName) {

    File initialFile = new File(appInfo.getSourceDir());
    File finalFile = new File(folderName, apkName);

    try {
        FileInputStream inStream = new FileInputStream(initialFile);
        FileOutputStream outStream = new FileOutputStream(finalFile);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();

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

    return finalFile;
}

Now create an intent using the File which you copied.

public static Intent getShareIntent(File file) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    intent.setType("application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    return intent;
}

Finally you can share the app,

ApplicationInfo app = getApplicationContext().getApplicationInfo();
File newFile = copyFile(app, Environment.getExternalStorageDirectory(), "[Expected name].apk");

Intent shareIntent = getShareIntent(newFile);
startActivity(Intent.createChooser(shareIntent, "Sharing [Appname] Apk"));

Upvotes: 3

Related Questions