Skip to content

Commit

Permalink
Added support of "content://", "assets://", "drawable://" schemes for
Browse files Browse the repository at this point in the history
image URIs from the box. Included ExtendedImageDownloader into the
library, use it as default downloader.
  • Loading branch information
nostra13 committed Feb 2, 2013
1 parent 861ddfe commit 4a336e8
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.nostra13.universalimageloader.core.assist.MemoryCacheUtil;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.ExtendedImageDownloader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

/**
Expand Down Expand Up @@ -56,9 +56,9 @@ public static MemoryCacheAware<String, Bitmap> createMemoryCache(int memoryCache
return memoryCache;
}

/** Create default implementation of {@link ImageDownloader} */
public static ImageDownloader createImageDownloader() {
return new URLConnectionImageDownloader();
/** Create default implementation of {@link ImageDownloader} - {@link ExtendedImageDownloader} */
public static ImageDownloader createImageDownloader(Context context) {
return new ExtendedImageDownloader(context);
}

/** Create default implementation of {@link BitmapDisplayer} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static class Builder {
private boolean loggingEnabled = false;

public Builder(Context context) {
this.context = context;
this.context = context.getApplicationContext();
}

/**
Expand Down Expand Up @@ -323,7 +323,7 @@ public Builder discCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
/**
* Sets utility which will be responsible for downloading of image.<br />
* Default value -
* {@link com.nostra13.universalimageloader.core.DefaultConfigurationFactory#createImageDownloader()
* {@link com.nostra13.universalimageloader.core.DefaultConfigurationFactory#createImageDownloader(Context)
* DefaultConfigurationFactory.createImageDownloader()}
* */
public Builder imageDownloader(ImageDownloader imageDownloader) {
Expand Down Expand Up @@ -381,7 +381,7 @@ private void initEmptyFiledsWithDefaultValues() {
memoryCache = DefaultConfigurationFactory.createMemoryCache(memoryCacheSize, denyCacheImageMultipleSizesInMemory);
}
if (downloader == null) {
downloader = DefaultConfigurationFactory.createImageDownloader();
downloader = DefaultConfigurationFactory.createImageDownloader(context);
}
if (defaultDisplayImageOptions == null) {
defaultDisplayImageOptions = DisplayImageOptions.createSimple();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.nostra13.universalimageloader.core.download;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;

/**
* Image downloader which supports retrieving images from {@linkplain ContentProvider content providers}
* (<b>"content://..."</b>), assets (<b>"assets://..."</b>) and drawables (<b>"drawable://..."</b>).
*
* @author Sergey Tarasevich
* @created 02.02.2013
*/
public class ExtendedImageDownloader extends URLConnectionImageDownloader {

protected static final String SCHEME_CONTENT = ContentResolver.SCHEME_CONTENT;
protected static final String SCHEME_ASSETS = "assets";
protected static final String SCHEME_DRAWABLE = "drawable";

private static final String SCHEME_ASSETS_PREFIX = SCHEME_ASSETS + "://";
private static final String SCHEME_DRAWABLE_PREFIX = SCHEME_DRAWABLE + "://";

protected Context context;

public ExtendedImageDownloader(Context context) {
this.context = context;
}

@Override
protected InputStream getStreamFromOtherSource(URI imageUri) throws IOException {
String protocol = imageUri.getScheme();
if (SCHEME_CONTENT.equals(protocol)) {
return getStreamFromContent(imageUri);
} else if (SCHEME_ASSETS.equals(protocol)) {
return getStreamFromAssets(imageUri);
} else if (SCHEME_DRAWABLE.equals(protocol)) {
return getStreamFromDrawable(imageUri);
} else {
return super.getStreamFromOtherSource(imageUri);
}
}

/** Retrieves {@link InputStream} of image by URI (image is accessed using {@link ContentResolver}) */
protected InputStream getStreamFromContent(URI imageUri) throws IOException {
ContentResolver res = context.getContentResolver();
Uri uri = Uri.parse(imageUri.toString());
return res.openInputStream(uri);
}

/** Retrieves {@link InputStream} of image by URI (image is located in assets of application) */
protected InputStream getStreamFromAssets(URI imageUri) throws IOException {
String filePath = imageUri.toString().substring(SCHEME_ASSETS_PREFIX.length()); // Remove "assets://" prefix from image URI
return context.getAssets().open(filePath);
}

/** Retrieves {@link InputStream} of image by URI (image is located in drawable resources of application) */
protected InputStream getStreamFromDrawable(URI imageUri) {
String drawableIdString = imageUri.toString().substring(SCHEME_DRAWABLE_PREFIX.length()); // Remove "drawable://" prefix from image URI
int drawableId = Integer.parseInt(drawableIdString);
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(drawableId);
Bitmap bitmap = drawable.getBitmap();

ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, os);
return new ByteArrayInputStream(os.toByteArray());
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
package com.nostra13.universalimageloader.core.download;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

/**
* Provides retrieving of {@link InputStream} of image by URI.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public abstract class ImageDownloader {

protected static final String PROTOCOL_FILE = "file";

protected static final String PROTOCOL_HTTP = "http";
protected static final String PROTOCOL_HTTPS = "https";
protected static final String PROTOCOL_FTP = "ftp";

protected static final int BUFFER_SIZE = 8 * 1024; // 8 Kb

/** Retrieves {@link InputStream} of image by URI. Image can be located as in the network and on local file system. */
public InputStream getStream(URI imageUri) throws IOException {
String scheme = imageUri.getScheme();
if (PROTOCOL_HTTP.equals(scheme) || PROTOCOL_HTTPS.equals(scheme) || PROTOCOL_FTP.equals(scheme)) {
return getStreamFromNetwork(imageUri);
} else if (PROTOCOL_FILE.equals(scheme)) {
return getStreamFromFile(imageUri);
} else {
return getStreamFromOtherSource(imageUri);
}
}

/**
* Retrieves {@link InputStream} of image by URI from other source. Should be overriden by successors to implement
* image downloading from special sources (not local file and not web URL).
*/
protected InputStream getStreamFromOtherSource(URI imageUri) throws IOException {
return null;
}

/** Retrieves {@link InputStream} of image by URI (image is located in the network) */
protected abstract InputStream getStreamFromNetwork(URI imageUri) throws IOException;

/** Retrieves {@link InputStream} of image by URI (image is located on the local file system or SD card) */
protected InputStream getStreamFromFile(URI imageUri) throws IOException {
return new BufferedInputStream(imageUri.toURL().openStream(), BUFFER_SIZE);
}
package com.nostra13.universalimageloader.core.download;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

/**
* Provides retrieving of {@link InputStream} of image by URI.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public abstract class ImageDownloader {

protected static final String SCHEME_FILE = "file";

protected static final String SCHEME_HTTP = "http";
protected static final String SCHEME_HTTPS = "https";
protected static final String SCHEME_FTP = "ftp";

protected static final int BUFFER_SIZE = 8 * 1024; // 8 Kb

/** Retrieves {@link InputStream} of image by URI. Image can be located as in the network and on local file system. */
public InputStream getStream(URI imageUri) throws IOException {
String scheme = imageUri.getScheme();
if (SCHEME_HTTP.equals(scheme) || SCHEME_HTTPS.equals(scheme) || SCHEME_FTP.equals(scheme)) {
return getStreamFromNetwork(imageUri);
} else if (SCHEME_FILE.equals(scheme)) {
return getStreamFromFile(imageUri);
} else {
return getStreamFromOtherSource(imageUri);
}
}

/**
* Retrieves {@link InputStream} of image by URI from other source. Should be overriden by successors to implement
* image downloading from special sources (not local file and not web URL).
*/
protected InputStream getStreamFromOtherSource(URI imageUri) throws IOException {
return null;
}

/** Retrieves {@link InputStream} of image by URI (image is located in the network) */
protected abstract InputStream getStreamFromNetwork(URI imageUri) throws IOException;

/** Retrieves {@link InputStream} of image by URI (image is located on the local file system or SD card) */
protected InputStream getStreamFromFile(URI imageUri) throws IOException {
return new BufferedInputStream(imageUri.toURL().openStream(), BUFFER_SIZE);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package com.nostra13.universalimageloader.core.download;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLConnection;

import com.nostra13.universalimageloader.core.assist.FlushedInputStream;

/**
* Default implementation of ImageDownloader. Uses {@link URLConnection} for image stream retrieving.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class URLConnectionImageDownloader extends ImageDownloader {

/** {@value} */
public static final int DEFAULT_HTTP_CONNECT_TIMEOUT = 5 * 1000; // milliseconds
/** {@value} */
public static final int DEFAULT_HTTP_READ_TIMEOUT = 20 * 1000; // milliseconds

private int connectTimeout;
private int readTimeout;

public URLConnectionImageDownloader() {
this(DEFAULT_HTTP_CONNECT_TIMEOUT, DEFAULT_HTTP_READ_TIMEOUT);
}

public URLConnectionImageDownloader(int connectTimeout, int readTimeout) {
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}

@Override
public InputStream getStreamFromNetwork(URI imageUri) throws IOException {
URLConnection conn = imageUri.toURL().openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
return new FlushedInputStream(new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE));
}
package com.nostra13.universalimageloader.core.download;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLConnection;

import com.nostra13.universalimageloader.core.assist.FlushedInputStream;

/**
* Implementation of ImageDownloader which uses {@link URLConnection} for image stream retrieving.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class URLConnectionImageDownloader extends ImageDownloader {

/** {@value} */
public static final int DEFAULT_HTTP_CONNECT_TIMEOUT = 5 * 1000; // milliseconds
/** {@value} */
public static final int DEFAULT_HTTP_READ_TIMEOUT = 20 * 1000; // milliseconds

private int connectTimeout;
private int readTimeout;

public URLConnectionImageDownloader() {
this(DEFAULT_HTTP_CONNECT_TIMEOUT, DEFAULT_HTTP_READ_TIMEOUT);
}

public URLConnectionImageDownloader(int connectTimeout, int readTimeout) {
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}

@Override
public InputStream getStreamFromNetwork(URI imageUri) throws IOException {
URLConnection conn = imageUri.toURL().openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
return new FlushedInputStream(new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE));
}
}

0 comments on commit 4a336e8

Please sign in to comment.