Skip to content

Instantly share code, notes, and snippets.

Created July 10, 2011 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1074178 to your computer and use it in GitHub Desktop.
Save anonymous/1074178 to your computer and use it in GitHub Desktop.
public class ImageContentProvider extends ContentProvider {
private static final String URI_PREFIX = "content://com.xianguo.image/";
public static String constructUri(String imageUrl) {
return URI_PREFIX + Uri.encode(imageUrl);
}
public static String deconstructUri(String imageUrl) {
if (imageUrl == null || !imageUrl.startsWith(URI_PREFIX)) {
return imageUrl;
}
imageUrl = imageUrl.substring(URI_PREFIX.length());
return Uri.decode(imageUrl);
}
public static String deconstructUri(Uri uri) {
String imageUrl = uri.toString();
return deconstructUri(imageUrl);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
String imageUrl = deconstructUri(uri);
ImageCache imageCache = ImageCache.getInstance(getContext());
if (!imageCache.containsInDisk(imageUrl)) {
byte[] imageData = downloadImage(imageUrl);
if (imageData != null) {
imageCache.cacheToDisk(imageUrl, imageData);
}
}
String path = imageCache.getFilePathInDisk(imageUrl);
if (path != null) {
File file = new File(path);
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}
return null;
}
/**
* Download From Internet
* @param imageUrl
*/
protected byte[] downloadImage(String imageUrl) {
try {
return HttpClient.doRequest(imageUrl, null);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public int delete(Uri uri, String s, String[] as) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
throw new UnsupportedOperationException("Not supported by this provider");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment