【问题标题】:Write a file world readable on smartphone's memory在智能手机的内存中写入一个可读的文件世界
【发布时间】:2013-09-02 08:31:00
【问题描述】:

由于我的应用程序,我正在尝试访问我填写的文件。我将其存储在手机内存中,而不是应用程序的内存中。

我的问题是当我在我的电脑上打开它时文件不存在(通过 USB 连接手机)。但是当我直接从我的智能手机打开它时它是可用的并且不是空的。

我需要此文件在我的计算机上可用。

在我的清单上:

READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE

我试过这种方式:

FileWriter outFile = new FileWriter(Environment.getExternalStoragePublicDirectory("my_app/data/file.txt");
PrintWriter out = new PrintWriter(outFile);
out.append("text");
out.flush();
out.close();

还有这种方式:

 File file = Environment.getExternalStoragePublicDirectory("my_app/data/file.txt");
 FileOutputStream outputStream = context.openFileOutput(file, Context.MODE_WORLD_READABLE);
 PrintStream printStream = new PrintStream(outputStream);
 printStream.println(text);
 printStream.close();

另一种方式..

File file = Environment.getExternalStoragePublicDirectory("my_app/data/file.txt");
FileWriter outFile = new FileWriter(file);
BufferedWriter buf = new BufferedWriter(outFile);
buf.write("hey");
buf.close();
outFile.close();

其中一些解决方案有效,但仅在我的手机上,所以它不是世界可读的,其他解决方案根本不起作用......

OutPutStreamWriterFileWriterBufferedWriterFileOutputStreamPrintWriter……有什么区别?我必须选择哪一个?

如何创建我的公共文件以便直接从我的计算机访问它?

感谢您的帮助!

【问题讨论】:

  • 可能是因为其他地方还在营业?
  • 不,这不是问题

标签: android storage android-file


【解决方案1】:

试试这个:

String toWrite = "save me to disk";
File dir = new File(Environment.getExternalStorageDirectory(), INSTALLATION_DIRECTORY); 
if (!dir.exists()) {
    dir.mkdirs();
}
File file = new File(dir, INSTALLATION_FILE);
FileOutputStream out = new FileOutputStream(file );
out.write(toWrite.getBytes());
out.close();

【讨论】:

  • 不,它仍然不起作用。我的文件已创建,文本已写在我的手机上,但是当我尝试在我的 PC 上查看相同的文件时,该文件夹为空...
  • 这个方法已经过我的测试并且有效。所以问题可能出在电脑连接上。您是否使用 ADB shell 看到该文件?
  • 是的,我可以在 Android Debug Monitor 中看到该文件,但在我的文件资源管理器中看不到它。
  • 短搜索产生了这些结果,试试他们的建议:stackoverflow.com/questions/7391432/…stackoverflow.com/questions/7429087/…
  • 它对我有用:context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+"my_path")))); 感谢您的宝贵时间!
猜你喜欢
  • 2021-06-21
  • 2022-11-18
  • 2010-09-07
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 2023-03-18
相关资源
最近更新 更多