【问题标题】:Can't check if file on sdcard exists无法检查 sdcard 上的文件是否存在
【发布时间】:2013-04-01 13:07:01
【问题描述】:

我正在尝试简单检查文件是否存在。我在这里看到了类似的问题,但它们没有帮助。当我运行我的应用程序时,应用程序崩溃并且我收到消息“不幸的是,fileCheck1 已停止”。我在模拟器和智能手机上都遇到了这个错误。

我的代码:

package com.example.fileCheck1;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

import java.io.File;

public class MyActivity extends Activity {

    TextView msgText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        msgText = (TextView) findViewById(R.id.textView);
        String Path = Environment.getExternalStorageDirectory().getPath()+"/ping.xml";
        File file = getBaseContext().getFileStreamPath(Path);
        if(file.exists()){
            msgText.setText("Found");
        }
        if(!file.exists()){
            msgText.setText("Not Found");
        }
    }
}

在我的清单中这样的权限:

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

提前致谢。

【问题讨论】:

    标签: android file


    【解决方案1】:

    我认为问题就在这里:

    getBaseContext()
    

    分配给 NULL 的位置。你真的不需要这条线。您可以通过

    轻松实现您的目标
    String path = Environment.getExternalStorageDirectory().getPath() + "/ping.xml";
    File f = new File(path);
    if (f.exists()) {
       // do your stuff
    }
    else {
      // do your stuff
    }
    

    更新:

    如果您或其他人拥有三星 Galaxy S3,请按照@Raghunandan 的回答,因为在这种情况下getExternalStorageDirectory() 返回内部存储器。

    【讨论】:

    • 感谢您的解释。我发现问题就在那里,但我不知道为什么。
    【解决方案2】:

    我有三星 Galaxy s3 和 android 4.1.2。我的内部手机内存命名为 sdcard0 和我的外部卡 extSdCard。

    Environment.getExternalStorageDirectory()
    

    所以上面返回的是sdcard0的路径,即手机内存

    所以得到你可以使用下面的实际路径

    String externalpath = new String();
    String internalpath = new String();
    
    public  void getExternalMounts() {
    Runtime runtime = Runtime.getRuntime();
    try
    {
    Process proc = runtime.exec("mount");
    InputStream is = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    String line;
    
    BufferedReader br = new BufferedReader(isr);
    while ((line = br.readLine()) != null) {
        if (line.contains("secure")) continue;
        if (line.contains("asec")) continue;
    
        if (line.contains("fat")) {//external card
            String columns[] = line.split(" ");
            if (columns != null && columns.length > 1) {
                externalpath = externalpath.concat("*" + columns[1] + "\n");
            }
    } 
            else if (line.contains("fuse")) {//internal storage
            String columns[] = line.split(" ");
            if (columns != null && columns.length > 1) {
                internalpath = internalpath.concat(columns[1] + "\n");
            }
        }
    }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
      System.out.println("Path  of sd card external............"+externalpath);
      System.out.println("Path  of internal memory............"+internalpath);
    }
    

    一旦找到路径

        File file = new File(internalpath+"/ping.xml");// internalpath or external path
        if(file.exists()){
            msgText.setText("Found");
        }
        else{
            msgText.setText("Not Found");
        }
    

    更新:

    不推荐使用上述解决方案。可能效果不好。 Environment.getExternalStorageDirectory() 将始终返回外部存储的路径。在大多数情况下,它是一个 SD 卡。

    来自文档

    公共静态文件getExternalStorageDirectory()

    在 API 级别 1 中添加 返回主外部存储目录。 如果已安装此目录,则当前可能无法访问 由用户在其计算机上,已从设备中删除,或 发生了其他一些问题。您可以确定其当前状态 使用 getExternalStorageState()。

    注意:不要被这里的“外部”一词所迷惑。这个目录 最好将其视为媒体/共享存储。它是一个文件系统 可以容纳相对大量的数据,并且可以共享 所有应用程序(不强制执行权限)。传统上这是 SD 卡,但它也可以作为内置存储实现 不同于受保护的内部存储的设备,并且可以 作为文件系统安装在计算机上。

    【讨论】:

    • Environment.getExternalStorageDirectory() emmm,它不应该总是返回外部存储路径吗?无论如何这都不是问题,它在我的索尼和模拟器上正确返回路径。问题在于崩溃。无论如何谢谢你
    • Environment.getExternalStorageDirectory() 返回 sdcard0 的路径,在我的例子中它实际上是手机内存。在这种情况下,您可以使用上述代码获取实际路径。
    • 不错。我不知道银河 s3 的问题。很高兴知道。我会更新 asnwer(仍然不知道谁以及为什么不赞成我,有趣)
    • 否,Environment.getExternalStorageDirectory() 总是返回外部存储的路径。问题是您实际上并不想要“外部存储”(实际上,今天通常是通过根据需要从内部闪存芯片借用空间来实现的),而是供应商可选的 SD 卡插槽。这不是在不是主要“外部存储”的手机上找到它的 API
    【解决方案3】:

    检查一下:

        File file = new File(Environment.getExternalStorageDirectory()+"/ping.xml");
        if(file.exists()){
            msgText.setText("Found");
        }
        else{
            msgText.setText("Not Found");
        }
    

    【讨论】:

    • 代码不用解释了..没什么大不了的..不明白..就一行...
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2021-12-28
    • 2017-04-21
    • 2011-09-12
    • 2023-01-20
    • 2013-06-23
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多