0

I'm currently using this code to store a file in Android:

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/invoices";
File file = new File(dir, "Invoice.pdf");

This works perfectly fine in Genymotion emulator because ? but when I deploy the app to an Android phone, it doesn't work.

Can anyone explain why this maybe, or hint me to the right direction please, thanks in advance.

almost a beginner
  • 1,622
  • 2
  • 20
  • 41

2 Answers2

1

You should post all you did because from your code nobody can understand what you are exactly doing. However, take a look at the following links:
1. http://codetheory.in/android-saving-files-on-internal-and-external-storage/.
At the link mentioned above it explains you how to save in both internal and external SDcard.
2. http://developer.android.com/training/basics/data-storage/files.html.
Second link is from Google and it is a small brief about what you should do.
In all the cases don't forget about the permissions.

EDIT:
Below I attached a piece of code that it can help you:

public static File getNewFile(String fileName) throws IOException {
    File file = null;

    if (name == null) {
        name = "temp_folder";
    }
    if (getInternalFilesDir() == null || !getInternalFilesDir().isDirectory()) {
        file = null;
    } else {
        file = new File(getInternalFilesDir() + File.separator + TEMP_FOLDER + File.separator + fileName);
        if (file.getParentFile() != null && ! file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        if (file exists()) {
           file.delete();
        }
        file.createNewFile();
        if (! file.exists()) {
            throw new IOException("Unable to create file " + name);
        }
    }
    return file;
}


getInternalFilesDir is exactly that:
Save files in internal directory
//in the manifest now:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     other stuff
</manifest>
Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • Sorry for late reply, but I think you mis-understood my question: I was asking why the code works in emulator but not in the actual device? Isn't the emulator suppose to emulate the device so that we can test it there before deployment? Shouldn't the emulator produce the same result as the physical device? – almost a beginner Sep 24 '15 at 04:46
  • First, did you fix the issue?. Secondly, I was telling you what it works for me. – Andrei T Sep 24 '15 at 08:44
  • I added the permission in the manifest but did not fix the issue, that being said, I will try to use your code as template and see if it works for me is well. Thanks – almost a beginner Sep 24 '15 at 11:34
1

Add Permission in manifest file <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And Try This to Save File/Image

Community
  • 1
  • 1
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • I put the permissions in the manifest file but got the same result, no file or directory is created, any suggestions how I can force an error report? Manifest Code: – almost a beginner Sep 24 '15 at 06:31