Convert Android vector drawable to launcher icon PNG

浪尽此生 提交于 2019-12-02 11:23:30

All you need to do is:

  1. Render the Vector in an ImageView
  2. Get the Bitmap from the ImageView
  3. Save the Bitmap to a PNG file

render the Vector in an ImageView:

//--------------------------------------------------------------------------------
    ImageView image = R.findViewById(R.id.imageView);//get ImageView
//--------------------------------------------------------------------------------

get the Bitmap from the ImageView:

//--------------------------------------------------------------------------------
    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();//get Bitmap
    SaveImage(bitmap,"my_png_file");//save it
//--------------------------------------------------------------------------------

Save the Bitmap to a PNG file:

//--------------------------------------------------------------------------------
    private void SaveImage(Bitmap finalBitmap, String filename)
    {
           String root = Environment.getExternalStorageDirectory().toString();
           File myDir = new File(root + "/saved_images");
           myDir.mkdirs();
           Random generator = new Random();
           int n = 10000;
           n = generator.nextInt(n);

           String fname = filename +".png";
           File file = new File (myDir, fname);
           if (file.exists ()) file.delete ();
           try {
               FileOutputStream out = new FileOutputStream(file);
               finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
               out.flush();
               out.close();
               sendBroadcast(new Intent(
                   Intent.ACTION_MEDIA_MOUNTED,
                   Uri.parse("file://" + Environment.getExternalStorageDirectory())));

           } catch (Exception e) {
               e.printStackTrace();
           }
    }//SaveImage
    //--------------------------------------------------------------------------------
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!