gpt4 book ai didi

android - 尝试使用 Fresco 从 uri 获取位图

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:26 27 4
gpt4 key购买 nike

当我使用 ImagePipeline 使用 Fresco 获取 Bitmap 时,不理解行为。当我调试我的代码时,它正在执行 onNewResultImplonFailureImpl 并且当我运行应用程序时它不工作意味着它没有被调用 onFailureImplonNewResultImpl(我在运行应用程序时使用 ToastLog 检查它)。我看过这个SO Question and take ref from it 还有from Fresco's doc.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case ACTION_OPEN_GALLERY:
mImageCaptureUri = data.getData();
if (mImageCaptureUri != null) {
commentImgView.setImageURI(mImageCaptureUri);//mImageCaptureUri is working fine
try {
imageRequest = ImageRequestBuilder
.newBuilderWithSource(mImageCaptureUri)
.setRequestPriority(Priority.HIGH)
.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
.build();
dataSource = imagePipeline.fetchDecodedImage(imageRequest, CommentActivity.this);
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
protected void onNewResultImpl(@Nullable Bitmap bitmap) {
if (bitmap != null) {
bmp = Bitmap.createBitmap(bitmap);
Log.d("Bitmap ","after callback");
Toast.makeText(CommentActivity.this,"has bitmap",Toast.LENGTH_SHORT).show();
} else {
Log.d("Bitmap is null ","after callback");
Toast.makeText(CommentActivity.this,"bitmap is null",Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
Log.d("Bitmap ","after callback failure");
Toast.makeText(CommentActivity.this,"Failure",Toast.LENGTH_SHORT).show();
}
}, CallerThreadExecutor.getInstance());
} catch (Exception e){
e.printStackTrace();
} finally {
if (dataSource != null) {
dataSource.close();
}
}
}
}
}
}

注意:我试图从 jpg 图像 中获取位图,而不是从任何动画 gif 图像中获取位图

最佳答案

我删除了 tryfinally block ,并关闭了 onNewResultImplonFailureImpl 中的 Datasource

代码 fragment

ImageRequest imageRequest = ImageRequestBuilder
.newBuilderWithSource(mImageCaptureUri)
.setAutoRotateEnabled(true)
.build();

ImagePipeline imagePipeline = Fresco.getImagePipeline();
final DataSource<CloseableReference<CloseableImage>>
dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);

dataSource.subscribe(new BaseBitmapDataSubscriber() {

@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
if (dataSource.isFinished() && bitmap != null){
Log.d("Bitmap","has come");
bmp = Bitmap.createBitmap(bitmap);
dataSource.close();
}
}

@Override
public void onFailureImpl(DataSource dataSource) {
if (dataSource != null) {
dataSource.close();
}
}
}, CallerThreadExecutor.getInstance());

关于android - 尝试使用 Fresco 从 uri 获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32949914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com