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

View file

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