diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/FileUtils.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/FileUtils.java index 652eb6fd4..e1fe098ac 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/FileUtils.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/FileUtils.java @@ -9,6 +9,9 @@ import com.panda3ds.pandroid.app.PandroidApplication; import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.OutputStream; public class FileUtils { public static final String MODE_READ = "r"; @@ -28,18 +31,38 @@ public static String getName(String path) { return parseFile(path).getName(); } - public static boolean createFolder(String path, String name){ + public static String getPrivatePath(){ + File file = getContext().getFilesDir(); + if (!file.exists()){ + file.mkdirs(); + } + return file.getAbsolutePath(); + } + + public static boolean exists(String path){ + return parseFile(path).exists(); + } + + public static boolean createDir(String path, String name){ DocumentFile folder = parseFile(path); + if (folder.findFile(name) != null) + return true; + return folder.createDirectory(name) != null; + } - if (folder.findFile(name) != null){ + public static boolean createFile(String path, String name){ + DocumentFile folder = parseFile(path); + if (folder.findFile(name) != null) return true; - } + return folder.createFile("application/octet-stream", name) != null; + } - return folder.createDirectory(name) != null; + public static InputStream getInputStream(String path) throws FileNotFoundException { + return getContext().getContentResolver().openInputStream(parseFile(path).getUri()); } - public static String getPrivatePath(){ - return getContext().getFilesDir().getAbsolutePath(); + public static OutputStream getOutputStream(String path) throws FileNotFoundException { + return getContext().getContentResolver().openOutputStream(parseFile(path).getUri()); } public static void makeUriPermanent(String uri, String mode) { diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/GameUtils.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/GameUtils.java index 49d4f6f2d..32f7c2a5d 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/GameUtils.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/utils/GameUtils.java @@ -13,8 +13,8 @@ import com.panda3ds.pandroid.app.PandroidApplication; import com.panda3ds.pandroid.data.game.GameMetadata; -import java.io.File; -import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Objects; @@ -81,11 +81,13 @@ private static synchronized void saveAll() { public static void setGameIcon(String id, Bitmap icon) { try { - File file = new File(FileUtils.getPrivatePath()+"/cache_icons/", id+".png"); - file.getParentFile().mkdirs(); - FileOutputStream o = new FileOutputStream(file); - icon.compress(Bitmap.CompressFormat.PNG, 100, o); - o.close(); + String appPath = FileUtils.getPrivatePath(); + FileUtils.createDir(appPath, "cache_icons"); + FileUtils.createFile(appPath+"/cache_icons/", id+".png"); + + OutputStream output = FileUtils.getOutputStream(appPath+"/cache_icons/"+id+".png"); + icon.compress(Bitmap.CompressFormat.PNG, 100, output); + output.close(); } catch (Exception e){ Log.e(Constants.LOG_TAG, "Error on save game icon: ", e); } @@ -93,9 +95,13 @@ public static void setGameIcon(String id, Bitmap icon) { public static Bitmap loadGameIcon(String id) { try { - File file = new File(FileUtils.getPrivatePath()+"/cache_icons/"+id+".png"); - if (file.exists()) - return BitmapFactory.decodeFile(file.getAbsolutePath()); + String path = FileUtils.getPrivatePath()+"/cache_icons/"+id+".png"; + if (FileUtils.exists(path)) { + InputStream stream = FileUtils.getInputStream(path); + Bitmap image = BitmapFactory.decodeStream(stream); + stream.close(); + return image; + } } catch (Exception e){ Log.e(Constants.LOG_TAG, "Error on load game icon: ", e); }