How to get .apk file of an application in android

/**
 * @param destPath path where you want to save the apk file
 * @return <b>true</b> if apk file is created successfully <br/>
 * <b>false</b> if apk file is not created
 <br/> <b>Note: </b>This method requires permission <b>android.permission.WRITE_EXTERNAL_STORAGE</b>
 */
 public boolean saveApkFile(String destPath)
 {
 boolean success = false;
 try
 {
 String packagename = getPackageName();

 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.applicationInfo.packageName!=null)
 {
 if(info.activityInfo.applicationInfo.packageName.toString().trim().equalsIgnoreCase(packagename))
 {
 String apkFIlePath = info.activityInfo.applicationInfo.publicSourceDir;
 success = copyFile(apkFIlePath,destPath);
 }
 }
 }
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 return success;
 }

The function copyFile is below

/**
 * @param srcFilePath path of file which you have to copy
 * @param destFilePath path of file to which you want to copy the above file
 * @return <b>true</b> if file was copied successfully <br/>
 * <b>false</b> if file cannot be copied <br/><br/>
 *
 * <b>Note:</b> This requires permission <b>"android.permission.WRITE_EXTERNAL_STORAGE"</b>
 */
 public static boolean copyFile(String srcFilePath, String destFilePath)
 {
 boolean success = false;

 if(srcFilePath!=null && destFilePath!=null)
 {
 srcFilePath = srcFilePath.trim();
 destFilePath = destFilePath.trim();

 if(!srcFilePath.equals("") && !destFilePath.equals("") && !srcFilePath.equals(destFilePath))
 {
 File srcFile = new File(srcFilePath);

 if(srcFile.exists() && srcFile.isFile())
 {
 try
 {
 File destFile = new File(destFilePath);
 InputStream in = new FileInputStream(srcFile);
 OutputStream out = new FileOutputStream(destFile);

// Transfer bytes from in to out
 byte[] buf = new byte[1024];
 int len;
 while ((len = in.read(buf)) > 0)
 {
 out.write(buf, 0, len);
 }

 in.close();
 out.close();
 success = true;
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 }
 }
 return success;
 }

Advertisements

Tags: ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: