AndroidアプリのPicassoをフレスコに置き換えようとしています。フレスコを使用してビットマップを単にロードする方法がわかりません。
ピカソを使って、私は次のようにします。
Bitmap poster = Picasso.with(getActivity())
.load(url)
.resize(Utils.convertDpToPixel(WIDTH,HEIGHT))
.centerCrop()
.get();
このフレスコでビットマップを作成する方法を理解できませんでした。何か案は?
フレスコが言ったように:
パイプラインへのリクエストがデコードされた画像に対するものである場合-Androidビットマップの場合、使いやすいBaseBitmapDataSubscriberを利用できます。
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
// You can use the bitmap in only limited ways
// No need to do any cleanup.
}
@Override
public void onFailureImpl(DataSource dataSource) {
// No cleanup required here.
}
},
executor);
OnNewResultImplメソッドのスコープ内にない変数にビットマップを割り当てることはできません。
http://frescolib.org/docs/datasources-datasubscribers.html#_
私のコード:
public void setDataSubscriber(Context context, Uri uri, int width, int height){
DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableBitmap>>() {
@Override
public void onNewResultImpl(
DataSource<CloseableReference<CloseableBitmap>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<CloseableBitmap> imageReference = dataSource.getResult();
if (imageReference != null) {
final CloseableReference<CloseableBitmap> closeableReference = imageReference.clone();
try {
CloseableBitmap closeableBitmap = closeableReference.get();
Bitmap bitmap = closeableBitmap.getUnderlyingBitmap();
if(bitmap != null && !bitmap.isRecycled()) {
//you can use bitmap here
}
} finally {
imageReference.close();
closeableReference.close();
}
}
}
@Override
public void onFailureImpl(DataSource dataSource) {
Throwable throwable = dataSource.getFailureCause();
// handle failure
}
};
getBitmap(context, uri, width, height, dataSubscriber);
}
/**
*
* @param context
* @param uri
* @param width
* @param height
* @param dataSubscriber
*/
public void getBitmap(Context context, Uri uri, int width, int height, DataSubscriber dataSubscriber){
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
if(width > 0 && height > 0){
builder.setResizeOptions(new ResizeOptions(width, height));
}
ImageRequest request = builder.build();
DataSource<CloseableReference<CloseableImage>>
dataSource = imagePipeline.fetchDecodedImage(request, context);
dataSource.subscribe(dataSubscriber, UiThreadExecutorService.getInstance());
}
これには、FrescoのCacheKeyを直接使用します。
public class DownloadVideoThumbnail extends AsyncTask<String, Void, Bitmap> {
private ImageView bmImage;
private Bitmap bitmapVideo;
private Context context;
public DownloadVideoThumbnail(Context context, ImageView bmImage) {
this.bmImage = (ImageView) bmImage;
this.context = context;
}
protected Bitmap doInBackground(String... urls) {
String urlStr = urls[0];
if (readFromCacheSync(urlStr) == null) {
try {
//Your method call here
bitmapVideo = retriveVideoFrameFromVideo(urlStr);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
} else {
bitmapVideo = readFromCacheSync(urlStr);
}
return null;
}
protected void onPostExecute(Bitmap result) {
if (bitmapVideo != null) {
//Load your bitmap here
bmImage.setImageBitmap(bitmapVideo);
bmImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}
public void cacheBitmap(Bitmap bitmap, String url) {
try {
CacheKey cacheKey = new SimpleCacheKey(url);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Fresco.getImagePipelineFactory().getMainFileCache().insert(cacheKey, new WriterCallback() {
@Override
public void write(OutputStream outputStream) throws IOException {
outputStream.write(byteArray);
}
});
} catch (IOException cacheWriteException) {
}
}
public static Bitmap readFromCacheSync(String imageUrl) {
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(imageUrl), null);
StagingArea stagingArea = StagingArea.getInstance();
EncodedImage encodedImage = stagingArea.get(cacheKey);
if (encodedImage != null) {
return BitmapFactory.decodeStream(encodedImage.getInputStream());
}
try {
return BitmapFactory.decodeStream(readFromDiskCache(cacheKey));
} catch (Exception e) {
return null;
}
}
private static InputStream readFromDiskCache(final CacheKey key) throws IOException {
try {
FileCache fileCache = ImagePipelineFactory.getInstance().getMainFileCache();
final BinaryResource diskCacheResource = fileCache.getResource(key);
if (diskCacheResource == null) {
FLog.v(TAG, "Disk cache miss for %s", key.toString());
return null;
}
PooledByteBuffer byteBuffer;
final InputStream is = diskCacheResource.openStream();
FLog.v(TAG, "Successful read from disk cache for %s", key.toString());
return is;
} catch (IOException ioe) {
return null;
}
}
public Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
else
mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
if (bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
cacheBitmap(bitmap, videoPath);
}
} catch (Exception e) {
e.printStackTrace();
throw new Throwable(
"Exception in retriveVideoFrameFromVideo(String videoPath)"
+ e.getMessage());
} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}
}
これには、Frescoの画像パイプラインを直接使用します。
http://frescolib.org/docs/using-image-pipeline.html
あなたが私の質問を気にしなくても、ここでの動機は何ですか?なぜビットマップ自体が必要なのですか?