Skip to content

Commit

Permalink
Apply SAF for write icon file
Browse files Browse the repository at this point in the history
replace fs/io by SAF implementation
  • Loading branch information
GabrielBRDeveloper committed Dec 21, 2023
1 parent 8b93ef7 commit cad4174
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,21 +81,27 @@ 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);
}
}

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);
}
Expand Down

0 comments on commit cad4174

Please sign in to comment.