Skip to content

Instantly share code, notes, and snippets.

@nesquena
Last active July 10, 2021 03:36
Show Gist options
  • Save nesquena/d84f30b14288c786772b to your computer and use it in GitHub Desktop.
Save nesquena/d84f30b14288c786772b to your computer and use it in GitHub Desktop.
VanGogh Image Loading Library (Re-creating Picasso)
public class MyVanGoghActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crappy_picasso);
// Downloading image and displaying it
// Passing into imageview
// Write it to disk
// Passing into imagebutton
// Setting it as the bg of a view
imageView = (ImageView) findViewById(R.id.imageView);
String url = "https://i.imgur.com/tGbaZCY.jpg";
VanGogh.load(url, new OnImageListener() {
public void onImageLoaded(Bitmap b) {
imageView.setImageBitmap(b);
}
}
}
}
public class VanGogh {
// TODO Things to add if we had more time
// Check for the network connectivity
// Resize the bitmap efficiently loading
// Caching (Memory, File cache)
// -- Memory (Bitmap store to "image_cache/<url>"
// -- Disk (Bitmap store to file "image_cache/<url>")
// Placeholder
// Fitting based on dimensions
// Scaling image
// Check if the url is valid
// Handle exceptions during download
// Add logging mechanisms
public interface OnImageListener {
public void onImageLoaded(Bitmap b, int res);
}
public static void load(String urlStr, OnImageListener listener) {
// Start the background task
new DownloadImageTask(urlStr, listener).execute();
}
// Convenience method for loading into an imageView
public static void load(String urlStr, final ImageView imageView) {
load(urlStr, new OnImageListener() {
@Override
public void onImageLoaded(Bitmap b, int res) {
imageView.setImageBitmap(b);
}
});
}
// Background Thread
// 1. From the UIThread,
// we need start a task
// 1b. Pass data to
// bg thread (url)
// 2. Run some task in bg
// 3. Pass the data back
// to the UIThread
public static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
String url;
OnImageListener listener;
public DownloadImageTask(String url, OnImageListener listener) {
this.url = url;
this.listener = listener;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
// 1. Create an asynchronous thread
// 2. Check for networking
// 3. Create a connection
URL urlO = null;
InputStream stream = null;
try {
urlO = new URL(url);
HttpURLConnection conn = (HttpURLConnection)
urlO.openConnection();
// 4. Open the input stream
conn.connect();
stream = conn.getInputStream();
// 5. Download and Decode data
Bitmap bitmap = BitmapFactory.decodeStream(stream);
// 6. Close the stream
stream.close();
/// Return Bitmap
return bitmap;
} catch (IOException e) {
e.printStackTrace();
// Smart exception loggin
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
// 7. Call the listener
if (listener != null) {
listener.onImageLoaded(bitmap, 200);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment