简体   繁体   English

从SD卡读取

[英]Reading from SD card

I am trying to read a simple text file from my SD card using Java, and I can't seem to be able to open it. 我正在尝试使用Java从我的SD卡中读取一个简单的文本文件,但似乎无法打开它。 I am using Eclipse for Mobile Developers on Ubuntu 12.04. 我正在Ubuntu 12.04上使用Eclipse for Mobile Developers。 Here's the code in the onCreate() method of the main activity: 这是主要活动的onCreate()方法中的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String status = "Hello, is it me you're looking for?";
    String storageState = Environment.getExternalStorageState();

    File dir = Environment.getExternalStorageDirectory();
    File f = new File(dir, "textTest.txt");

    if (f.exists()) {
        status = "File is found!";
    } else {
        status = "File is not found!";
    }

    this.text1 = (TextView) findViewById(R.id.textView1);
    text1.setText(status.subSequence(0, status.length()));
}

Also, here are some of the constructors I tried for File f , none of them worked: 另外,这是我为File f尝试过的一些构造File f ,但它们都不起作用:

File f = new File(dir, "textTest.txt");
File f = new File("/sdcard/textTest.txt");
File f = new File(dir, "mnt/sdcard/textTest.txt");

Finally, I checked the external storage state using the storageState string above, and the value returned by Environment.getExternalStorageState() is mounted . 最后,我使用上面的storageState字符串检查了外部存储状态,并mountedEnvironment.getExternalStorageState()返回的值。 The only lead I have is I think there might be something to do with whether the SD card is readable or not, but that's just a guess. 我唯一的线索是,我认为SD卡是否可读可能与它有关,但这只是一个猜测。 Any ideas? 有任何想法吗?

Added: In case you haven't guessed, the TextView object reads " File not found! " when I launch the activity. 补充:如果您没有猜到,当我启动活动时, TextView对象将显示“ File not found! ”。

Added 2: Ok, I just tried something that worked. 补充2:好的,我只是尝试了一些有效的方法。 When I connect the phone via USB, two folders related to the phone open: one which has all of the folders from the internal storage (data, DCIM... etc) and the folder which I assume is connected to the SD card, since I added the files through my laptop card reader and then input it into the phone. 当我通过USB连接手机时,会打开两个与手机相关的文件夹:一个文件夹具有内部存储器中的所有文件夹(数据,DCIM等),而我认为该文件夹已连接至SD卡,因为我通过笔记本电脑读卡器添加了文件,然后将其输入到手机中。 However, when I copy the file "testText.txt" into the first folder (which I assumed is related to the internal storage), the constructor File f = new File("/sdcard/textTest.txt"); 但是,当我将文件“ testText.txt”复制到第一个文件夹(我认为与内部存储有关)时,构造函数File f = new File("/sdcard/textTest.txt"); works. 作品。 Howcome? 怎么会?

Based on comments.. 根据评论..

Add this to your AndroidManifest.xml 将此添加到您的AndroidManifest.xml

android.permission.READ_EXTERNAL_STORAGE

Also if you do somethings with WRITE operation add this permission 另外,如果您使用WRITE操作执行操作,请添加此权限

android.permission.WRITE_EXTERNAL_STORAGE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM