Reputation: 201
I have created a share Button for my app that it will share my entire application through Intent.. It works like this:
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), 0);
File srcFile = new File(ai.publicSourceDir);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile));
startActivity(Intent.createChooser(share, "PersianCoders"));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}
Now this will send my application with my package name.. I need to rename it with my application name before sharing.. Like this for example:
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(
packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA));
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
String shareName = appName+"_"+"v"+version;
Is there a way to rename my package name with this "shareName" String ?
Upvotes: 2
Views: 1484
Reputation: 2829
As @CommonsWare mentioned,You can do it by creating a backup of your application into a path ,then share it from that path:
public void shareApp(String package_name) {
try {
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(
packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA));
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
String shareName = appName+"_"+"v"+version;
File f1;
File f2 = null;
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<?> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
for (Object object : pkgAppsList) {
ResolveInfo info = (ResolveInfo) object;
if (info.activityInfo.packageName.equals(package_name)) {
f1 = new File(info.activityInfo.applicationInfo.publicSourceDir);
try {
f2 = new File(Environment.getExternalStorageDirectory().toString() + "/Apps");
f2.mkdirs();
f2 = new File(f2.getPath() + "/" + shareName + ".apk");
f2.createNewFile();
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage() + " in the specified directory.");
} catch (IOException e) {
System.out.println(e.getMessage());}
}
}
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f2));
startActivity(Intent.createChooser(share, "PersianCoders"));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}
}
And call it wherever you like:
shareApp(getPackageName());
Upvotes: 3
Reputation: 1006674
Is there a way to rename my package name with this "shareName" String ?
Not directly. There is no "use this alternative filename" option in ACTION_SEND
.
You can:
Make a copy of the original file, under the new filename, and share that copy, or
Implement a ContentProvider
, one that returns the file's content when it receives a Uri
with the new filename
The latter is more complex but also solves your Uri.fromFile()
problem: that will not work once you raise your targetSdkVersion
to 24 or higher, when running on an Android 7.0+ device.
Upvotes: 0