Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.