Skip to content

Instantly share code, notes, and snippets.

@JohnZavyn
Created August 5, 2016 17:53
Show Gist options
  • Save JohnZavyn/af6ffc3d58443311c4a3bc8eba949932 to your computer and use it in GitHub Desktop.
Save JohnZavyn/af6ffc3d58443311c4a3bc8eba949932 to your computer and use it in GitHub Desktop.
Extract the icon from an APK file
import net.dongliu.apk.parser.ApkParser;
import net.dongliu.apk.parser.bean.Icon;
import org.apache.log4j.Logger;
import java.io.*;
/** Services for managing APK (Adroid Application Package) objects. */
public class ApkService
{
/** The {@link Logger}. */
private static final Logger LOGGER = Logger.getLogger(ApkService.class);
/**
* Extract the icon from an APK file.
*
* @param apkParser the {@link ApkParser}
*
* @return the icon temp {@link File}
*/
public File saveIcon(ApkParser apkParser)
{
OutputStream out = null;
File file = null;
try
{
final Icon iconFile = apkParser.getIconFile();
String iconPath = iconFile.getPath();
String extension = iconPath.substring(iconPath.lastIndexOf('.'));
file = File.createTempFile("icon", extension);
out = new FileOutputStream(file);
out.write(iconFile.getData());
out.flush();
}
catch (Exception e)
{
throw new IllegalStateException("Problem extracting icon", e);
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
LOGGER.error(e);
}
}
}
return file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment