mirror of
https://github.com/izzy2lost/Shipwright-Android.git
synced 2026-07-05 15:19:26 -07:00
FileSelector
This commit is contained in:
@@ -49,8 +49,8 @@
|
||||
<!-- Allow access to the vibrator -->
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
|
||||
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
|
||||
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<!-- if you want to capture audio, uncomment this. -->
|
||||
<!-- <uses-permission android:name="android.permission.RECORD_AUDIO" /> -->
|
||||
@@ -80,7 +80,7 @@
|
||||
android:configChanges="layoutDirection|locale|orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
|
||||
android:preferMinimalPostProcessing="true"
|
||||
android:exported="true"
|
||||
android:screenOrientation="landscape"
|
||||
android:screenOrientation="userLandscape"
|
||||
>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
@@ -18,14 +18,54 @@ import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import android.util.Log;
|
||||
|
||||
//This class is the main SDLActivity
|
||||
//This class is the main SDLActivity and just sets up a bunch of default files
|
||||
public class MainActivity extends SDLActivity{
|
||||
|
||||
private static final int STORAGE_PERMISSION_REQUEST_CODE = 1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//Check if folder/file exists and if it doesn't, create it
|
||||
// Check if storage permissions are granted
|
||||
if (hasStoragePermission()) {
|
||||
setupFiles();
|
||||
} else {
|
||||
requestStoragePermission();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check if storage permission is granted
|
||||
private boolean hasStoragePermission() {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED &&
|
||||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
// Request storage permission
|
||||
private void requestStoragePermission() {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||
STORAGE_PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
|
||||
// Handle permission request result
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == STORAGE_PERMISSION_REQUEST_CODE) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
setupFiles();
|
||||
} else {
|
||||
// Permission denied, handle accordingly (e.g., show a message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setupFiles(){
|
||||
File externalAssetsDir = new File(getExternalFilesDir(null), "assets");
|
||||
if (!externalAssetsDir.exists()) {
|
||||
try {
|
||||
@@ -57,4 +97,51 @@ public class MainActivity extends SDLActivity{
|
||||
|
||||
}
|
||||
|
||||
private native void nativeHandleSelectedFile(String filePath);
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == 0 && resultCode == RESULT_OK) {
|
||||
Uri selectedFileUri = data.getData();
|
||||
String fileName = "ZELOOTD.z64";
|
||||
File destinationDirectory = getExternalFilesDir(null); // The second argument can specify a subdirectory, or you can pass null to use the root directory.
|
||||
File destinationFile = new File(destinationDirectory, fileName);
|
||||
|
||||
if (destinationDirectory != null) {
|
||||
try {
|
||||
InputStream in = getContentResolver().openInputStream(selectedFileUri);
|
||||
OutputStream out = new FileOutputStream(destinationFile);
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
in.close();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
nativeHandleSelectedFile(destinationFile.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
public void openFilePicker() {
|
||||
// Create an Intent to open the file picker dialog
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.setType("*/*");
|
||||
|
||||
// Start the file picker dialog
|
||||
startActivityForResult(intent, 0);
|
||||
}
|
||||
|
||||
// Check if external storage is available and writable
|
||||
private boolean isExternalStorageWritable() {
|
||||
String state = Environment.getExternalStorageState();
|
||||
return Environment.MEDIA_MOUNTED.equals(state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user