Так что в значительной степени у меня есть этот код, который я создал для загрузки аудиофайла на мой VPS. Он работает на Android 9 и ниже, однако, когда я использовал телефоны своих друзей и родственников, которые работают на Android 10, он не работает.
Действительно смущен тем, почему существует другая файловая система или что-то? Может ли кто-нибудь, пожалуйста, указать мне в правильном направлении?
Заранее извиняюсь!
@SuppressLint("SetTextI18n")
@Override
/**
* Method for File Name, Size and uploading to the server.
* Validation has also been added to the method for exceptions.
*
* Suppress Lint "SetTextIl8n" is used to allow hard coded strings.
* This will be updated in a future iteration to fully utilise strings.xml
*/
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 101) {
if (data == null) {
//no data present
return;
}
String name;
Uri uri = data.getData();
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
// variables for size and name
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
// Logs file file name
Log.d("FILE O NAME", returnCursor.getString(nameIndex));
name = returnCursor.getString(nameIndex);
//
int dot = name.lastIndexOf(".");
String type = name.substring(dot + 1);
// Validation for Audio files.
if (type.contentEquals("mp3") || type.contentEquals("m4a") || type.contentEquals("ogg") || type.contentEquals("aac")) {
// Else throw Toast to the user to select an audio file
} else {
Toast.makeText(this, "Choose an Audio file - MP3 / M4A / OGG / AAC formats only.", Toast.LENGTH_LONG).show();
return;
}
// Set the text for the filename to be the name + the size of the file in MB
filename.setText(name + " File size:" + (((returnCursor.getLong(sizeIndex)) / 1000) / 1000) + "MB");
File file = new File(getCacheDir(), name);
// Maximum buffer size allowed to be uploaded.
int maxBufferSize = 1 * 1024 * 1024;
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
Log.e("InputStream Size", "Size " + inputStream);
int bytesAvailable = inputStream.available();
// int bufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
final byte[] buffers = new byte[bufferSize];
// File output stream initialised to file
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
while ((read = inputStream.read(buffers)) != -1) {
outputStream.write(buffers, 0, read);
}
// Log for file size
Log.e("File Size", "Size " + file.length());
// close input and output stream
inputStream.close();
outputStream.close();
// initialise files to file
files = file;
// logs for file path
Log.e("File Path", "Path " + file.getPath());
file.length();
// logs for file size
Log.e("File Size", "Size " + file.length());
// Exception handling for file not found exception
} catch (FileNotFoundException e) {
e.printStackTrace();
// Input Output Exception error
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Мне удается открыть селектор файлов и с кодом ниже.
/**
* Method to open file selector, allows user to navigate for MP3 file.
*/
private void showFileSelector() {
Intent intent = new Intent();
//sets the select file to all types of files
intent.setType("*/*");
//allows to select data and return it
intent.setAction(Intent.ACTION_GET_CONTENT);
//starts new activity to select file and return data
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), 101);
}