1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.graphics.drawable.tests; 18 19import android.content.res.Resources; 20import android.graphics.Bitmap; 21import android.util.Log; 22 23import java.io.File; 24import java.io.FileOutputStream; 25import java.io.IOException; 26 27 28public class DrawableUtils { 29 30 private static final String LOGTAG = DrawableUtils.class.getSimpleName(); 31 32 // This is only for debugging or golden image (re)generation purpose. 33 public static void saveVectorDrawableIntoPNG(Resources resource, Bitmap bitmap, int resId, 34 String filename) throws IOException { 35 // Save the image to the disk. 36 FileOutputStream out = null; 37 try { 38 String outputFolder = "/sdcard/temp/"; 39 File folder = new File(outputFolder); 40 if (!folder.exists()) { 41 folder.mkdir(); 42 } 43 String fileTitle = "unname"; 44 if (resId >= 0) { 45 String originalFilePath = resource.getString(resId); 46 File originalFile = new File(originalFilePath); 47 String fileFullName = originalFile.getName(); 48 fileTitle = fileFullName.substring(0, fileFullName.lastIndexOf(".")); 49 if (filename != null) { 50 fileTitle += "_" + filename; 51 } 52 } else if (filename != null) { 53 fileTitle = filename; 54 } 55 String outputFilename = outputFolder + fileTitle + "_golden.png"; 56 File outputFile = new File(outputFilename); 57 if (!outputFile.exists()) { 58 outputFile.createNewFile(); 59 } 60 61 out = new FileOutputStream(outputFile, false); 62 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 63 Log.v(LOGTAG, "Write test No." + outputFilename + " to file successfully."); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } finally { 67 if (out != null) { 68 out.close(); 69 } 70 } 71 } 72} 73