使用MediaStore在Android Q中创建/复制文件 [英] Create/Copy File in Android Q using MediaStore
问题描述
我正在尝试找到一种方法,该方法可以处理除媒体文件(图片/视频/音频)以外的任何文件的创建和复制,以将其从Android Q的内部存储中的一个位置复制到另一个位置.我的应用文件夹,我希望这些文件夹移到下载文件夹"或我可以在内部存储中创建的某个目录,然后移动它们.
I am trying to find method which can handle create and copy of any file except Media files (Picture/Video/Audio) to copy from one place to other in internal storage in Android Q. In this I have my file created in my app folder and I want those to move to Download Folder or some directory which I can create in Internal storage and then move their.
我搜索并发现下面的代码已修改,但缺少一些使其可行的东西.可以帮忙吗.
I searched and found modified below code but missing some thing to make it workable. Can some one help.
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();
}
以上是完整代码,因为我收到错误消息未知URL".缺什么?请帮忙.
Is above complete code as I'm getting error 'Unknown URL'. What is missing? Please help.
推荐答案
1.创建和写入文件
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.查找和读取文件
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.查找并覆盖文件
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();
}
}
}
}
});
- 读/写模式: https://developer.android.com/training/data-storage/shared/media#open-file-descriptor
演示: https://www.youtube.com/watch?v=idsUMiWjfnM
希望这对您有所帮助.
这篇关于使用MediaStore在Android Q中创建/复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!