I‘m new to Android programming and couldn‘t find a good solution for my problem yet. In my App users can select photos from their gallery which are then used in a Cardview Layout for different categorys in the App which the user can create on his own. By now I‘m able to get Uri of the selected photo and can display it. But how can I save the photo to my App to make sure it‘s always there even though it gets deleted from the gallery?
Asked
Active
Viewed 39 times
-2
-
Just copy the file and save it to where you want. [Copy file](https://stackoverflow.com/questions/9292954/how-to-make-a-copy-of-a-file-in-android) [Storage](https://developer.android.com/guide/topics/data/data-storage) – Neo Shen Aug 20 '18 at 10:30
-
But where do I save it best? – Nimey Aug 20 '18 at 10:40
-
What is your purpose of showing the photos when they are deleted? Save them in private/external storage might be enough. – Neo Shen Aug 21 '18 at 16:22
1 Answers
0
Ref: How to make a copy of a file in android?
To copy a file and save it to your destination path you can use the method below.
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
On API 19+ you can use Java Automatic Resource Management: public static void copy(File src, File dst) throws IOException { t
ry (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
}

Devzone
- 73
- 8