Last active
December 4, 2018 23:24
-
-
Save ashafa/b2f9eeb4977d4df0ab5e5fc4a1933212 to your computer and use it in GitHub Desktop.
This file contains 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
ackage cr.canopy.tunde.canopy.manager; | |
import android.content.res.AssetManager; | |
import android.support.v4.app.FragmentActivity; | |
import android.util.Log; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Random; | |
public class ImagesManager { | |
private static File createFileFromInputStream(File cacheDir, InputStream inputStream, String fileName) { | |
try { | |
File f = new File(cacheDir + "/" +fileName); | |
OutputStream outputStream = new FileOutputStream(f); | |
byte buffer[] = new byte[1024]; | |
int length = 0; | |
while ((length=inputStream.read(buffer)) > 0) { | |
outputStream.write(buffer,0, length); | |
} | |
outputStream.close(); | |
inputStream.close(); | |
return f; | |
} catch (IOException e) { | |
Log.e(">", e.getMessage()); | |
} | |
return null; | |
} | |
public static List<File> getThumbnailImages(FragmentActivity appCompatActivity) throws IOException { | |
AssetManager assetManager = appCompatActivity.getAssets(); | |
String [] files = assetManager.list("images"); | |
List<File> result = new ArrayList<>(); | |
for (String f : files) { | |
File file = createFileFromInputStream( | |
appCompatActivity.getCacheDir(), | |
assetManager.open("images/" + f), f); | |
if (f.endsWith("thumbnail.jpg")) { | |
result.add(file); | |
} | |
} | |
return result; | |
} | |
public static List<File> getCuratedImages(FragmentActivity appCompatActivity, int count) throws IOException { | |
AssetManager assetManager = appCompatActivity.getAssets(); | |
String [] files = assetManager.list("images"); | |
ArrayList<String> pool = new ArrayList<>(Arrays.asList(files)); | |
List<File> result = new ArrayList<>(); | |
Random rand = new Random(); | |
for (int i = 0; i < count; i++) { | |
int randomIndex = rand.nextInt(pool.size()); | |
String f = pool.get(randomIndex); | |
if (!f.endsWith("thumbnail.jpg")) { | |
result.add(createFileFromInputStream( | |
appCompatActivity.getCacheDir(), | |
assetManager.open("images/" + f), f)); | |
} | |
pool.remove(randomIndex); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment