Apply SAF for write icon file

replace fs/io by SAF implementation
This commit is contained in:
Gabriel 2023-12-21 15:35:59 -04:00
parent 8b93ef76ac
commit cad4174845
2 changed files with 46 additions and 17 deletions

View file

@ -9,6 +9,9 @@ import androidx.documentfile.provider.DocumentFile;
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 class FileUtils {
return parseFile(path).getName();
}
public static boolean createFolder(String path, String name){
DocumentFile folder = parseFile(path);
if (folder.findFile(name) != null){
return true;
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;
}
public static String getPrivatePath(){
return getContext().getFilesDir().getAbsolutePath();
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;
}
public static InputStream getInputStream(String path) throws FileNotFoundException {
return getContext().getContentResolver().openInputStream(parseFile(path).getUri());
}
public static OutputStream getOutputStream(String path) throws FileNotFoundException {
return getContext().getContentResolver().openOutputStream(parseFile(path).getUri());
}
public static void makeUriPermanent(String uri, String mode) {

View file

@ -13,8 +13,8 @@ import com.panda3ds.pandroid.app.GameActivity;
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 @@ public class GameUtils {
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 class GameUtils {
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);
}