将文本文件保存到 Android 中的公共内存位置 [英] Saving a text file to public internal memory location in Android

查看:60
本文介绍了将文本文件保存到 Android 中的公共内存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,当发生各种用户操作时,我想在其中附加一个文本文件.我希望文本文件位于内部存储器中.如果用户使用 USB 电缆连接平板电脑,我希望文本文件也能在 Windows 资源管理器下可见.

I am writing an App, in which I would like to append a text file, as various user actions happen. I would like the text file to be located in internal memory. I would like the text file to also be visible under Windows Explorer, if the user connects the tablet with a usb cable.

我查看了许多示例,并且能够将文本文件写入内部存储器,但我似乎无法在文件管理器中显示该文件.

I have looked at many examples, and have been able to write a text file to internal memory, but I can not seem to make the file visible in File Manager.

有谁知道是否可以将文本文件写入内部存储器,并在 Windows 资源管理器下对用户可见?或者我需要使用 SD 卡而不是内部存储器吗?

Does anyone know if it is possible to write a text file to internal memory, and have it visible to the user under Windows Explorer? Or do I need to use an SD card instead of internal memory?

推荐答案

好的,我现在可以按照我的意愿工作了.它会将文本文件(并在每次写入时附加)保存到平板电脑/手机的不可移动的 DOWNLOAD 文件夹中.我的目标是最低 SDK 版本为 16 (4.1.2 Jellybean).

Ok, I have this working as I want it to, now. It will save the text file (and append it each time it is written) to the non-removable DOWNLOAD folder of a tablet/phone. My target was a minimum SDKversion of 16 (4.1.2 Jellybean).

我将在下面发布代码,供其他对此问题感到困惑的人使用.

I will post the code below, for anyone else who gets confused by this issue.

try {
File path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOWNLOADS);
File myFile = new File(path, "mytextfile.txt");
FileOutputStream fOut = new FileOutputStream(myFile,true);
    OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
    myOutWriter.append("the text I want added to the file");
    myOutWriter.close();
    fOut.close();

    Toast.makeText(this,"Text file Saved !",Toast.LENGTH_LONG).show();
    }

    catch (java.io.IOException e) {

    //do something if an IOException occurs.
    Toast.makeText(this,"ERROR - Text could't be
    added",Toast.LENGTH_LONG).show();
                      }

您还需要在清单中添加以下行:

You will also need the following line added to the Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

您需要导入:

import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.File;

这篇关于将文本文件保存到 Android 中的公共内存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆