【发布时间】:2021-06-12 08:07:32
【问题描述】:
(编辑)@blackapps 的回答:将“w”更改为“wt”。它有效!
//-------------------------------------------------------//
我跟着tutorial写了一个简单的文本编辑器,结果很奇怪。
我要实现的功能:一个记录我今天想吃什么的文本文件。
我第一次将“菠萝”写入文本文件。文本文件的内容是“菠萝”。但是,我第二次将“apple”写入文本文件。文本文件的内容是“applepple”。看起来只是替换了之前内容的前五个字符。
我的问题是:如何使内容记录“apple”而不是“applepple”?我想使用 SAF 和 URI。我不想使用直接路径来访问文件。
我在下载文件夹中创建和编辑文本文件。 我在 Android 11、targetSdkVersion 30 上运行该应用程序。 以下是代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--call this only once
// createFile(MediaStore.Downloads.INTERNAL_CONTENT_URI);
}
private String todayFood = "pineapple";//change here to apple
private static final int CREATE_FILE = 1;
private static final int OPEN_FILE = 2;
private void createFile(Uri pickerInitialUri) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "foodFile");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
startActivityForResult(intent, CREATE_FILE);
}
public void onOpenFileBtn(View v){//button click event
openFile(MediaStore.Downloads.INTERNAL_CONTENT_URI);
}
private void openFile(Uri pickerInitialUri) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
startActivityForResult(intent, OPEN_FILE);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == OPEN_FILE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
outputToFile(uri);
}
} else if (requestCode == CREATE_FILE && resultCode == Activity.RESULT_OK) {
System.out.println("create successful");
}
}
public void outputToFile(Uri fileUri) {
try {
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(fileUri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
fileOutputStream.write(todayFood.getBytes());
fileOutputStream.close();
pfd.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以下是权限:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
如果您能提供任何帮助,我将不胜感激。
//-------------------------------------------------------//
//--我尝试过的:
我尝试使用直接路径,它可以正常工作。我认为原因是它创建了一个新文件来替换原始文件。但是,我只是不想使用直接路径。
public void outputToFile(Uri fileUri) {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File f = new File(dir.getPath(), "foodFile.txt");
try {
FileOutputStream outputStream = new FileOutputStream(f);
outputStream.write(todayFood.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我试过用PrintWriter来清除内容,但它也需要直接路径先创建文件然后放入PrintWriter。
public void outputToFile(Uri fileUri) {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
try {
File f = new File(dir.getPath(), "foodFile.txt");
PrintWriter writer = new PrintWriter(f);
writer.print("");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我曾尝试使用OutputStream创建PrintWriter,但不起作用。
public void outputToFile(Uri fileUri) {
try {
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(fileUri, "w");
OutputStream outputStream = new FileOutputStream(pfd.getFileDescriptor());
PrintWriter writer = new PrintWriter(outputStream);
writer.print("");
writer.write("apple");
writer.close();
pfd.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我也尝试在另一个文件夹中创建和编辑文本文件,例如 Document 文件夹,但它也不起作用。
我曾尝试请求运行时权限,但它还有一个我现在无法解决的问题。
【问题讨论】:
-
把“w”改成“wt”。
-
谢谢你,它工作!我阅读了文档,但并没有解释太多。它只是说““rwt”模式意味着磁盘上支持查找的文件。”
-
W==写,t==截断。
标签: java android uri android-11 storage-access-framework