Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change apk filename when share it from application

Tags:

java

android

apk

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 ?

like image 885
Mahdi Avatar asked Nov 18 '25 01:11

Mahdi


1 Answers

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"));
like image 53
Muthukrishnan Rajendran Avatar answered Nov 20 '25 13:11

Muthukrishnan Rajendran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!