-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support of "content://", "assets://", "drawable://" schemes for
image URIs from the box. Included ExtendedImageDownloader into the library, use it as default downloader.
- Loading branch information
Showing
5 changed files
with
174 additions
and
97 deletions.
There are no files selected for viewing
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
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
77 changes: 77 additions & 0 deletions
77
library/src/com/nostra13/universalimageloader/core/download/ExtendedImageDownloader.java
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
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()); | ||
} | ||
} |
98 changes: 49 additions & 49 deletions
98
library/src/com/nostra13/universalimageloader/core/download/ImageDownloader.java
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
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); | ||
} | ||
} |
82 changes: 41 additions & 41 deletions
82
...ary/src/com/nostra13/universalimageloader/core/download/URLConnectionImageDownloader.java
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
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)); | ||
} | ||
} |