If you think the Android project ion listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.koushikdutta.ion.loader;
/*fromwww.java2s.com*/import android.content.Context;
import android.net.Uri;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.stream.InputStreamDataEmitter;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import java.io.InputStream;
/**
* Created by koush on 5/22/13.
*/publicclass ContentLoader extends StreamLoader {
@Override
public Future<BitmapInfo> loadBitmap(final Context context, final Ion ion, final String key, final String uri, finalint resizeWidth, finalint resizeHeight, finalboolean animateGif) {
if (!uri.startsWith("content:/"))
return null;
return super.loadBitmap(context, ion, key, uri, resizeWidth, resizeHeight, animateGif);
}
@Override
protected InputStream getInputStream(Context context, String uri) throws Exception {
return context.getContentResolver().openInputStream(Uri.parse(uri));
}
@Override
public Future<DataEmitter> load(final Ion ion, final AsyncHttpRequest request, final FutureCallback<LoaderEmitter> callback) {
if (!request.getUri().getScheme().startsWith("content"))
return null;
final InputStreamDataEmitterFuture ret = new InputStreamDataEmitterFuture();
ion.getHttpClient().getServer().post(new Runnable() {
@Override
publicvoid run() {
try {
InputStream stream = ion.getContext().getContentResolver().openInputStream(Uri.parse(request.getUri().toString()));
if (stream == null)
thrownew Exception("Unable to load content stream");
int available = stream.available();
InputStreamDataEmitter emitter = new InputStreamDataEmitter(ion.getHttpClient().getServer(), stream);
ret.setComplete(emitter);
callback.onCompleted(null,
new LoaderEmitter(emitter, available, LoaderEmitter.LOADED_FROM_CACHE, null, null));
}
catch (Exception e) {
ret.setComplete(e);
callback.onCompleted(e, null);
}
}
});
return ret;
}
}