Last active
February 4, 2016 22:19
-
-
Save gautier-levert/bb4b6a027e8d010ed353 to your computer and use it in GitHub Desktop.
Android TP3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.iut_amiens.imagelist; | |
import java.io.Serializable; | |
import java.net.URI; | |
import java.util.Arrays; | |
import java.util.List; | |
public class AnimalImage implements Serializable { | |
public static final List<AnimalImage> ANIMAL_IMAGE_LIST = Arrays.asList( | |
new AnimalImage("Baby Giraffe Face", URI.create("http://www.publicdomainpictures.net/pictures/110000/velka/baby-giraffe-face.jpg")), | |
new AnimalImage("Sleeping Polar Bear", URI.create("http://www.publicdomainpictures.net/pictures/110000/velka/sleeping-polar-bear.jpg")), | |
new AnimalImage("Koala Bear In Tree", URI.create("http://www.publicdomainpictures.net/pictures/110000/velka/koala-bear-in-tree.jpg")), | |
new AnimalImage("Rhinoceros", URI.create("http://www.publicdomainpictures.net/pictures/110000/velka/rhinoceros-14202086623vq.jpg")), | |
new AnimalImage("Alligator On Dock", URI.create("http://www.publicdomainpictures.net/pictures/110000/velka/alligator-on-dock.jpg")), | |
new AnimalImage("Baby Lamb", URI.create("http://www.publicdomainpictures.net/pictures/20000/velka/baby-lamb.jpg")), | |
new AnimalImage("Meerkat", URI.create("http://www.publicdomainpictures.net/pictures/10000/velka/2364-126824238033E9.jpg")), | |
new AnimalImage("Cat With Green Eyes", URI.create("http://www.publicdomainpictures.net/pictures/20000/velka/cat-with-green-eyes-871298226869aN0.jpg")), | |
new AnimalImage("Black Cow", URI.create("http://www.publicdomainpictures.net/pictures/10000/velka/1-1192354093.jpg")), | |
new AnimalImage("Mia", URI.create("http://www.publicdomainpictures.net/pictures/30000/velka/mia-1330985708p4N.jpg")) | |
); | |
private String title; | |
private String fileName; | |
private URI imageUrl; | |
public AnimalImage(String title, URI imageUrl) { | |
this.title = title; | |
this.imageUrl = imageUrl; | |
String path = imageUrl.getPath(); | |
fileName = path.substring(path.lastIndexOf('/') + 1, path.length()); | |
} | |
public String getTitle() { | |
return title; | |
} | |
public URI getImageUrl() { | |
return imageUrl; | |
} | |
public String getFileName() { | |
return fileName; | |
} | |
@Override | |
public String toString() { | |
return title; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.iut_amiens.imagelist; | |
import android.net.Uri; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
public class ImageCache { | |
private File imageCacheDirectory; | |
public ImageCache(File imageCacheDirectory) { | |
this.imageCacheDirectory = imageCacheDirectory; | |
} | |
public void download(AnimalImage image) throws IOException, InterruptedException { | |
File localFile = new File(imageCacheDirectory, image.getFileName()); | |
if (localFile.exists()) { | |
return; | |
} | |
HttpURLConnection connection = (HttpURLConnection) image.getImageUrl().toURL().openConnection(); | |
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { | |
throw new IOException("Server didn't reply correctly \n" + connection.getResponseMessage()); | |
} | |
BufferedInputStream input = null; | |
BufferedOutputStream output = null; | |
try { | |
input = new BufferedInputStream(connection.getInputStream()); | |
output = new BufferedOutputStream(new FileOutputStream(localFile)); | |
int data; | |
while ((data = input.read()) != -1) { | |
if (Thread.interrupted()) { | |
throw new InterruptedException(); | |
} | |
output.write(data); | |
} | |
output.flush(); | |
} finally { | |
if (input != null) { | |
try { | |
input.close(); | |
} catch (IOException ignored) { | |
} | |
} | |
if (output != null) { | |
try { | |
output.close(); | |
} catch (IOException ignored) { | |
} | |
} | |
} | |
} | |
public Uri getLocalData(AnimalImage image) { | |
File localFile = new File(imageCacheDirectory, image.getFileName()); | |
return Uri.fromFile(localFile); | |
} | |
public boolean isImageDownloaded(AnimalImage image) { | |
File localFile = new File(imageCacheDirectory, image.getFileName()); | |
return localFile.exists(); | |
} | |
public void deleteCache() { | |
for (File f : imageCacheDirectory.listFiles()) { | |
f.delete(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment