J'essaie de trouver une méthode qui peut gérer la création et la copie de n'importe quel fichier à l'exception des fichiers multimédias (image/vidéo/audio) à copier d'un endroit à un autre dans le stockage interne dans Android Q. Dans ce J'ai mon fichier créé dans mon dossier d'application et je veux que ceux-ci soient déplacés vers le dossier de téléchargement ou vers un répertoire que je peux créer dans le stockage interne, puis déplacer leur fichier.
J'ai cherché et trouvé le code modifié ci-dessous mais il manquait quelque chose pour le rendre réalisable. Quelqu'un peut-il aider.
ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "sam.txt");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
Uri uri = contentResolver.insert(MediaStore.Files.getContentUri("external"), contentValues);
try {
InputStream inputStream = contentResolver.openInputStream(uri);
OutputStream outputStream = new FileOutputStream(Environment.DIRECTORY_DOWNLOADS+"/");
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Est au-dessus du code complet car j'obtiens l'erreur "URL inconnue". Que manque-t-il? Veuillez aider.
1. Créer et écrire un fichier
createAndWriteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "menuCategory"); //file name
values.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain"); //file extension, will automatically add to file
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS + "/Kamen Rider Decade/"); //end "/" is not mandatory
Uri uri = getContentResolver().insert(MediaStore.Files.getContentUri("external"), values); //important!
OutputStream outputStream = getContentResolver().openOutputStream(uri);
outputStream.write("This is menu category data.".getBytes());
outputStream.close();
Toast.makeText(view.getContext(), "File created successfully", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(view.getContext(), "Fail to create file", Toast.LENGTH_SHORT).show();
}
}
});
2. Rechercher et lire un fichier
findAndReadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri contentUri = MediaStore.Files.getContentUri("external");
String selection = MediaStore.MediaColumns.RELATIVE_PATH + "=?";
String[] selectionArgs = new String[]{Environment.DIRECTORY_DOCUMENTS + "/Kamen Rider Decade/"};
Cursor cursor = getContentResolver().query(contentUri, null, selection, selectionArgs, null);
Uri uri = null;
if (cursor.getCount() == 0) {
Toast.makeText(view.getContext(), "No file found in \"" + Environment.DIRECTORY_DOCUMENTS + "/Kamen Rider Decade/\"", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
if (fileName.equals("menuCategory.txt")) {
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
uri = ContentUris.withAppendedId(contentUri, id);
break;
}
}
if (uri == null) {
Toast.makeText(view.getContext(), "\"menuCategory.txt\" not found", Toast.LENGTH_SHORT).show();
} else {
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
int size = inputStream.available();
byte[] bytes = new byte[size];
inputStream.read(bytes);
inputStream.close();
String jsonString = new String(bytes, StandardCharsets.UTF_8);
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("File Content");
builder.setMessage(jsonString);
builder.setPositiveButton("OK", null);
builder.create().show();
} catch (IOException e) {
Toast.makeText(view.getContext(), "Fail to read file", Toast.LENGTH_SHORT).show();
}
}
}
}
});
3. Rechercher et écraser le fichier
findAndWriteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri contentUri = MediaStore.Files.getContentUri("external");
String selection = MediaStore.MediaColumns.RELATIVE_PATH + "=?";
String[] selectionArgs = new String[]{Environment.DIRECTORY_DOCUMENTS + "/Kamen Rider Decade/"}; //must include "/" in front and end
Cursor cursor = getContentResolver().query(contentUri, null, selection, selectionArgs, null);
Uri uri = null;
if (cursor.getCount() == 0) {
Toast.makeText(view.getContext(), "No file found in \"" + Environment.DIRECTORY_DOCUMENTS + "/Kamen Rider Decade/\"", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
if (fileName.equals("menuCategory.txt")) { //must include extension
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
uri = ContentUris.withAppendedId(contentUri, id);
break;
}
}
if (uri == null) {
Toast.makeText(view.getContext(), "\"menuCategory.txt\" not found", Toast.LENGTH_SHORT).show();
} else {
try {
OutputStream outputStream = getContentResolver().openOutputStream(uri, "rwt"); //overwrite mode, see below
outputStream.write("This is overwritten data。\n你就不要想起我。".getBytes());
outputStream.close();
Toast.makeText(view.getContext(), "File written successfully", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(view.getContext(), "Fail to write file", Toast.LENGTH_SHORT).show();
}
}
}
}
});
J'espère que cela peut vous aider.