1 /* 2 * Copyright (C) 2023 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.scopedstorage.cts.device; 18 19 import android.os.FileUtils; 20 21 import androidx.test.platform.app.InstrumentationRegistry; 22 23 import java.io.File; 24 import java.io.FileOutputStream; 25 import java.io.IOException; 26 import java.io.InputStream; 27 28 public class FileCreationUtils { 29 createContentFromResource(int resourceId, File file)30 protected static void createContentFromResource(int resourceId, File file) throws IOException { 31 try (FileOutputStream fileOutputStream = new FileOutputStream(file); 32 InputStream in = InstrumentationRegistry.getInstrumentation().getContext() 33 .getResources().openRawResource(resourceId)) { 34 // Dump the image we have to external storage 35 FileUtils.copy(in, fileOutputStream); 36 // Sync file to disk to ensure file is fully written to the lower fs attempting to 37 // open for redaction. Otherwise, the FUSE daemon might not accurately parse the 38 // EXIF tags and might misleadingly think there are not tags to redact 39 fileOutputStream.getFD().sync(); 40 } 41 42 } 43 } 44