get Drawable From Url
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
class IOUtils {
private static final String LOG_TAG = "IOUtils";
public static final String PREFS_FILE = "javaeye.prefs";
public static Drawable getDrawableFromUrl(URL url) {
try {
InputStream is = url.openStream();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
return null;
}
private static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] b = new byte[4 * 1024];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Log.e(LOG_TAG, e.getMessage());
}
}
}
}
Related examples in the same category