Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';t检查SD卡上的文件是否存在_Android_File - Fatal编程技术网

Android Can';t检查SD卡上的文件是否存在

Android Can';t检查SD卡上的文件是否存在,android,file,Android,File,我想简单检查一下文件是否存在。我在这里看到了类似的问题,但没有帮助。当我运行应用程序时,应用程序崩溃,我收到消息“不幸的是,fileCheck1已停止”。我在模拟器和智能手机上都遇到了这个错误 我的代码: package com.example.fileCheck1; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.TextVi

我想简单检查一下文件是否存在。我在这里看到了类似的问题,但没有帮助。当我运行应用程序时,应用程序崩溃,我收到消息“不幸的是,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>


提前谢谢。

我想问题在于:

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()
返回内部内存。

请检查:

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

我有三星galaxy s3和安卓4.1.2。我的手机内存名为sdcard0,外卡名为extSdCard

Environment.getExternalStorageDirectory()
因此,上面返回sdcard0的路径,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");
    }
更新:


不建议使用上述解决方案。可能不太管用<代码>环境。getExternalStorageDirectory()将始终返回外部存储的路径。在大多数情况下,它是SD卡

从文件中

公共静态文件getExternalStorageDirectory()

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

注意:这里的“外部”一词不要混淆。这个目录 最好将其视为介质/共享存储。它是一个 可以保存相对大量的数据,并且可以跨多个服务器共享 所有应用程序(不强制执行权限)。传统上这是 SD卡,但它也可以作为 与受保护的内部存储器不同的设备,可以 作为文件系统安装在计算机上


谢谢你的解释。我发现问题就在那里,但我不知道为什么。Environment.getExternalStorageDirectory()emmm,它不应该总是返回外部存储路径吗?无论如何,这不是一个问题,它正确地返回了我的索尼和模拟器的路径。问题在于撞车。无论如何,感谢您的环境。getExternalStorageDirectory()返回了sdcard0的路径,在我的例子中,它实际上是手机内存。在这种情况下,您可以使用上述代码获得实际路径。我不知道galaxy s3的问题。很高兴知道。我将更新asnwer(仍然不知道谁和为什么否决我,很有趣)@Sajmon看看链接No,Environment.getExternalStorageDirectory()总是返回外部存储的路径。问题在于,您实际上并不想要“外部存储”(实际上,今天通常是通过从内部闪存芯片根据需要借用空间来实现),而是想要一个供应商可选的SD卡插槽。这并不是API在手机上发现的,因为它不是主要的“外部存储”,不需要解释代码。。那没什么大不了的。。。不明白。。只有一行。。。