Reputation: 1632
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.
Upvotes: 0
Views: 142
Reputation: 3083
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>
Upvotes: 1
Reputation: 10069
Add Permission in manifest file <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And Try This to Save File/Image
Upvotes: 1