Last active
April 2, 2019 12:01
-
-
Save f2face/8b9784b1696b3f62a625d7c893edfea7 to your computer and use it in GitHub Desktop.
[Android] A class to get image from a URL and find the vibrant color.
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
package com.example.app.package; // Your package namespace | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.AsyncTask; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import androidx.annotation.ColorInt; | |
import androidx.palette.graphics.Palette; | |
@SuppressWarnings("unused") | |
public class ImageFromUrl extends AsyncTask<Void, Void, Bitmap> { | |
private static final int DEFAULT_VIBRANT_COLOR = 0xCCCCCC; | |
private GetImageFromUrlListener getImageFromUrlListener; | |
private GetVibrantColorListener getVibrantColorListener; | |
private Bitmap bitmap; | |
private String imageUrl; | |
public ImageFromUrl(String imageUrl) { | |
this.imageUrl = imageUrl; | |
} | |
public void setImageUrl(String imageUrl) { | |
this.imageUrl = imageUrl; | |
} | |
public static Bitmap getBitmap(String imageUrl) { | |
InputStream in = null; | |
try { | |
in = new java.net.URL(imageUrl).openStream(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return BitmapFactory.decodeStream(in); | |
} | |
@Override | |
protected Bitmap doInBackground(Void... voids) { | |
try { | |
InputStream in = new java.net.URL(this.imageUrl).openStream(); | |
this.bitmap = BitmapFactory.decodeStream(in); | |
} catch (Exception e) { | |
// I have no idea what to do here. I think it's just fine to do nothing. | |
} | |
return this.bitmap; | |
} | |
@Override | |
protected void onPostExecute(Bitmap bitmap) { | |
super.onPostExecute(bitmap); | |
if (getImageFromUrlListener != null) { | |
getImageFromUrlListener.onFinishGettingImageFromUrl(bitmap); | |
} | |
if (getVibrantColorListener != null) { | |
Palette.from(bitmap).generate(p -> { | |
if (p != null) { | |
getVibrantColorListener.onVibrantColorGenerated(p.getVibrantColor(DEFAULT_VIBRANT_COLOR)); | |
} | |
else { | |
getVibrantColorListener.onVibrantColorGenerated(DEFAULT_VIBRANT_COLOR); | |
} | |
}); | |
} | |
} | |
public void setOnFinishGettingImageFromUrl(GetImageFromUrlListener listener) { | |
this.getImageFromUrlListener = listener; | |
} | |
public interface GetImageFromUrlListener { | |
void onFinishGettingImageFromUrl(Bitmap bitmap); | |
} | |
public void setOnGetVibrantColorListener(GetVibrantColorListener listener) { | |
this.getVibrantColorListener = listener; | |
} | |
public interface GetVibrantColorListener { | |
void onVibrantColorGenerated(@ColorInt int vibrantColor); | |
} | |
} |
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
// Make sure you're using Java 8. | |
// Read more: https://developer.android.com/studio/write/java8-support | |
// Initialize | |
ImageFromUrl imageFromUrl = new ImageFromUrl("https://example.com/image.jpg"); // "https://example.com/image.jpg" is the image URL. | |
// Do something when the image has been downloaded. | |
imageFromUrl.setOnFinishGettingImageFromUrl(bitmap -> { | |
imageView.setImageBitmap(bitmap); // Display image to an ImageView. | |
}); | |
// Do something when the vibrant color has been found. | |
imageFromUrl.setOnGetVibrantColorListener(vibrantColor -> { | |
// ... | |
}); | |
// Execute this process in the background thread. | |
imageFromUrl.execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment