2

I'm trying to make a simple check if the file exist. I saw similar questions here, but they didn't help. When I run my application, the app crashes and I got message "Unfortunatelly, fileCheck1 has stopped". I got this error both on emulator and smartphone.

My code:

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");
        }
    }
}

In my Manifest such permissions:

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

Thanks in advance.

krosh
  • 218
  • 4
  • 15

3 Answers3

3

I think that problem is here:

getBaseContext()

where it is assigned to NULL. You really don't need this line. You can simply achieve your goal with

String path = Environment.getExternalStorageDirectory().getPath() + "/ping.xml";
File f = new File(path);
if (f.exists()) {
   // do your stuff
}
else {
  // do your stuff
}

Update:

If you or someone else have Samsung Galaxy S3, please follow @Raghunandan's answer because in this case getExternalStorageDirectory() returns internal memory.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • 1
    Thanks for explanation. I figured out that the problem was there, but i didn't know why. – krosh Apr 01 '13 at 16:31
3

I have samsung galaxy s3 with android 4.1.2. My internal phone memory is named sdcard0 and my external card extSdCard.

Environment.getExternalStorageDirectory()

So the above returns the path of sdcard0 which is internal phone memory

So get the actual path you can use the below

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);
}

Once you get the path

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

UPDATE :

The above solution is not recommended. May not work well. Environment.getExternalStorageDirectory() will always return the path of External Storage. In most cases it is a Sdcard.

From the docs

public static File getExternalStorageDirectory ()

Added in API level 1 Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    Environment.getExternalStorageDirectory() emmm, shouldn't it always return external storage path? Any way this was not a problem, and it correctly return path on my Sony and emulatator. The problem was in crashing. Anyway thank you – krosh Apr 01 '13 at 16:42
  • Environment.getExternalStorageDirectory() returned the path of sdcard0 which was actually phone memory in my case. In such cases you can get the actual path using the above code. – Raghunandan Apr 01 '13 at 16:44
  • nice. i did not know about galaxy s3 issue. good to know. i will update asnwer(still dont know who and why downvoted me, interesting) – Simon Dorociak Apr 01 '13 at 18:36
  • @ Sajmon have a look at the link http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0 – Raghunandan Apr 01 '13 at 18:41
  • 1
    No, Environment.getExternalStorageDirectory() **always** returns the path of the **External Storage**. The problem is that **you don't actually want** the "External Storage" (which indeed, today is often implemented with space borrowed as needed from internal flash chips) but rather a vendor-optional SD card slot. And this is not the API to find that on a phone where that is not the primary "External Storage" – Chris Stratton Mar 14 '14 at 20:30
  • @ChrisStratton agreed. I gotta know about this after having a chat with commonsware and checking the docs. Gonna edit it – Raghunandan Mar 15 '14 at 01:17
  • @Raghunandan, I am facing issue with Android 6.0 it returns internal storage path with Environment.getExter‌​nalStorageDirectory() but I want external storage and I want to write file on it. Can you please help me in this? I tried all solution and feeling helpless now. – Anvesh Aug 17 '16 at 03:56
  • @Anvesh this answer is old.lot of changes have happened since the day I answered. Try posting a new question – Raghunandan Aug 17 '16 at 04:05
  • @Anvesh do look at Adopting Storage devices @ https://developer.android.com/about/versions/marshmallow/android-6.0.html – Raghunandan Aug 17 '16 at 04:53
0

check this:

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