citra_android: fix crash while load amiibo (#6373)

This commit is contained in:
hank121314 2023-03-30 20:13:20 +08:00 committed by GitHub
parent 506644b9d6
commit 979c6d9c55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View file

@ -4,6 +4,7 @@ import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Pair;
@ -28,6 +29,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.app.NotificationManagerCompat;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.FragmentActivity;
import org.citra.citra_emu.CitraApplication;
@ -568,10 +570,11 @@ public final class EmulationActivity extends AppCompatActivity {
}
private void onAmiiboSelected(String selectedFile) {
File file = new File(selectedFile);
boolean success = false;
try {
byte[] bytes = FileUtil.getBytesFromFile(file);
Uri uri = Uri.parse(selectedFile);
DocumentFile file = DocumentFile.fromSingleUri(this, uri);
byte[] bytes = FileUtil.getBytesFromFile(this, file);
success = NativeLibrary.LoadAmiibo(bytes);
} catch (IOException e) {
e.printStackTrace();

View file

@ -380,8 +380,9 @@ public class FileUtil {
return false;
}
public static byte[] getBytesFromFile(File file) throws IOException {
final long length = file.length();
public static byte[] getBytesFromFile(Context context, DocumentFile file) throws IOException {
final Uri uri = file.getUri();
final long length = FileUtil.getFileSize(context, uri.toString());
// You cannot create an array using a long type.
if (length > Integer.MAX_VALUE) {
@ -394,7 +395,7 @@ public class FileUtil {
int offset = 0;
int numRead;
try (InputStream is = new FileInputStream(file)) {
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
while (offset < bytes.length &&
(numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;