1

How to save a file in android? I have the following code:

 final File file = new File(cont.getExternalFilesDir(null), filename);
                if (!file.exists()) {
                    file.createNewFile();
                }

and I get

03-21 11:31:29.903: W/System.err(31668): java.io.IOException: open failed: ENOENT (No such file or directory)
03-21 11:31:29.903: W/System.err(31668):    at java.io.File.createNewFile(File.java:948)

exception My complete code for the saving of the file is the following:

final File file = new File(context.getExternalFilesDir(null), filename);
            if (!file.exists()) {
                // file.mkdirs();
                file.createNewFile();
            }
            final OutputStream output = new FileOutputStream(file);
            try {
                try {
                    final byte[] buffer = new byte[1024];
                    int read;
                    while ((read = input.read(buffer)) != -1)
                        output.write(buffer, 0, read);
                    output.flush();
                } finally {
                    output.close();
                }
                Log.i("save_file", "save to disk is ok");
            } catch (Exception e) {
                e.printStackTrace();
            }

I also have the permissions:

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

Can some one please point out what I'm I doing wrong, thanks

EDITED: The problem was the name of the file that i wanted to save, so it shouldn't be contain restricted chars, mine was in this format:"xxx/xxx.jpg" so '/' is not a valid char for a file name. Thanks

4
  • 1
    Check this Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) before context.getExternalFilesDir(null) Commented Mar 21, 2014 at 10:48
  • so if i have the phone connected to a usb on a pc it might not work? Commented Mar 21, 2014 at 11:00
  • it's becoz your SDcard not mounted to your phone when your phone connected with USB Commented Mar 21, 2014 at 11:02
  • go to this http://stackoverflow.com/questions/20547771/context-getexternalfilesdirnull-is-null and read this post Commented Mar 21, 2014 at 11:08

3 Answers 3

1

Better to Use Environment.getExternalStorageDirectory() instead of context.getExternalFilesDir(null) even though it seems same.

here is my code:

String extr = Environment.getExternalStorageDirectory().toString();
        File mFolder = new File(extr + "/MainFolder_Photo");

        if (!mFolder.exists()) {
            mFolder.mkdir();
        }

        String strF = mFolder.getAbsolutePath();
        mSubFolder = new File(strF + "/Pictures");

        if (!mSubFolder.exists()) {
            mSubFolder.mkdir();
        } 

        // check file are there or not;// if present delete

        try {


        File yourDir = new File(mSubFolder.getAbsoluteFile().toString());
        for (File f : yourDir.listFiles()) {

            if (f.length()>0){

        f.delete();

            }

        }
        } catch (Exception e) {
            // TODO: handle exception
        }
        // file name
        String s = "Puzzle_Photo.png";

        File f = new File(mSubFolder.getAbsolutePath(), s);

        strMyImagePath = f.getAbsolutePath();

        OutputStream output;
        try {
            output = new FileOutputStream(f);
            URL url = new URL("file://" + filePath);// your data
            InputStream input = url.openStream();
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                output.write(buffer, 0, bytesRead);
            }

            output.close();
            input.close();

            return true;

        } catch (Exception e) {

            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

Comments

1

This is the way to store file on SD card,

    File path = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/yourdir/");
        if (!path.exists()) {
            path.mkdirs();
        }

   File file = new File(path.getAbsolutePath() + "/filename
                 .txt");
      final OutputStream output = new FileOutputStream(file);
        try {
            try {
                final byte[] buffer = new byte[1024];
                int read;
                while ((read = input.read(buffer)) != -1)
                    output.write(buffer, 0, read);
                output.flush();
            } finally {
                output.close();
            }
            Log.i("save_file", "save to disk is ok");
        } catch (Exception e) {
            e.printStackTrace();
        }

i am not sure about your input file.

2 Comments

a actually need to store it on the phone and not on a SD card, I think that context.getExternalFilesDir(null) gives me the phone
sorry for mess, i will put shortly how to store on phone memory
1

this is the way to store file on phone memory,

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File dir = cw.getDir("EMD Systems", Context.MODE_PRIVATE);
        File Filedir = new File(dir + "/Zip");
        if (!Filedir.exists()) {
            Filedir.mkdirs();
        }
    File file = new File(Filedir.getAbsolutePath() + "/filename
             .txt");
  final OutputStream output = new FileOutputStream(file);
    try {
        try {
            final byte[] buffer = new byte[1024];
            int read;
            while ((read = input.read(buffer)) != -1)
                output.write(buffer, 0, read);
            output.flush();
        } finally {
            output.close();
        }
        Log.i("save_file", "save to disk is ok");
    } catch (Exception e) {
        e.printStackTrace();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.