1 /* 2 * Copyright (C) 2009 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 17 package android.provider.cts; 18 19 import android.content.Context; 20 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.OutputStream; 24 import java.util.ArrayList; 25 26 /** 27 * The Class FileCopyHelper is used to copy files from resources to the 28 * application directory and responsible for deleting the files. 29 * 30 * @see MediaStore_VideoTest 31 * @see MediaStore_Images_MediaTest 32 * @see MediaStore_Images_ThumbnailsTest 33 */ 34 public class FileCopyHelper { 35 /** The context. */ 36 private Context mContext; 37 38 /** The files added. */ 39 private ArrayList<String> mFilesList; 40 41 /** 42 * Instantiates a new file copy helper. 43 * 44 * @param context the context 45 */ FileCopyHelper(Context context)46 public FileCopyHelper(Context context) { 47 mContext = context; 48 mFilesList = new ArrayList<String>(); 49 } 50 51 /** 52 * Copy the file from the resources with a filename . 53 * 54 * @param resId the res id 55 * @param fileName the file name 56 * 57 * @return the absolute path of the destination file 58 */ copy(int resId, String fileName)59 public String copy(int resId, String fileName) { 60 InputStream source = null; 61 OutputStream target = null; 62 63 try { 64 source = mContext.getResources().openRawResource(resId); 65 target = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE); 66 67 byte[] buffer = new byte[1024]; 68 for (int len = source.read(buffer); len > 0; len = source.read(buffer)) { 69 target.write(buffer, 0, len); 70 } 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } finally { 74 try { 75 if (source != null) { 76 source.close(); 77 } 78 if (target != null) { 79 target.close(); 80 } 81 } catch (IOException e) { 82 // Ignore the IOException. 83 } 84 } 85 86 mFilesList.add(fileName); 87 return mContext.getFileStreamPath(fileName).getAbsolutePath(); 88 } 89 90 /** 91 * Delete all the files copied by the helper. 92 */ clear()93 public void clear(){ 94 for (String path : mFilesList) { 95 mContext.deleteFile(path); 96 } 97 } 98 } 99