When my app first starts, it lets the user select a profile picture. This can be done taking a photo at the moment, or selecting it from the gallery.
After the user gets a picture, this must be saved in the device's internal storage and will be used in the app as user's profile picture.
This process works fine, the user gets the picture and this is shown in an imageview before it's saved. But to save the image in the internal storage, I'm having some trouble. I have tried several ways to do it, and most of them seem to work. But when I try them, the picture is not being saved or at least I don't find the folder where it is being saved.
I have tried in these 3 ways:
First:
File directory = getDir("profile", Context.MODE_PRIVATE);
File mypath = new File(directory, "thumbnail.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
mybitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Second:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);
FileOutputStream fos = null;
try {
fos = openFileOutput("thumbnail.png", Context.MODE_PRIVATE);
fos.write(bytes.toByteArray());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Third:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);
File fileWithinMyDir = new File(getFilesDir(), "thumbnail.png");
try {
FileOutputStream fos = new FileOutputStream(fileWithinMyDir);
fos.write(bytes.toByteArray());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Suposedly, the image gets saved in the path: android/data/AppName/app_data/
but there is no folder created there. Anyway I have looked in other folders, but nothing.
EDIT-
In the first method, I've seen that is throwing an exception:
E/SAVE_IMAGE﹕ /data/data/com.example.myapp/app_profile/thumbnail.png: open failed: EISDIR (Is a directory)
java.io.FileNotFoundException: /data/data/com.example.myapp/app_profile/thumbnail.png: open failed: EISDIR (Is a directory)
Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
See Question&Answers more detail:
os