Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android - Update Image.getSize to use encoded image #14358

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import com.facebook.datasource.DataSubscriber;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.react.bridge.Arguments;
Expand Down Expand Up @@ -81,22 +82,23 @@ public void getSize(
Uri uri = Uri.parse(uriString);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build();

DataSource<CloseableReference<CloseableImage>> dataSource =
Fresco.getImagePipeline().fetchDecodedImage(request, mCallerContext);
DataSource<CloseableReference<PooledByteBuffer>> dataSource =
Fresco.getImagePipeline().fetchEncodedImage(request, mCallerContext);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fresco only supports limited source types for encoded image:
https://github.com/facebook/fresco/blob/v1.3.0/imagepipeline/src/main/java/com/facebook/imagepipeline/core/ProducerSequenceFactory.java#L169

For example: SOURCE_TYPE_LOCAL_CONTENT (content://) which is returned from RollCamera is not supported

Use ContentResolver.openInputStream together with BitmapUtil.decodeDimensions(...) for those not supported types.

Actually BitmapUtil.decodeDimensions probably has better performance than loading the fully encoded image since it only needs to decode the metadata. So I suggest use that when possible.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orenklein @foghina
See my approach at bood@ea4b4dd

I'll make another PR if it looks good.


DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
DataSubscriber<CloseableReference<PooledByteBuffer>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
@Override
protected void onNewResultImpl(
DataSource<CloseableReference<CloseableImage>> dataSource) {
DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
if (!dataSource.isFinished()) {
return;
}
CloseableReference<CloseableImage> ref = dataSource.getResult();
CloseableReference<PooledByteBuffer> ref = dataSource.getResult();
if (ref != null) {
EncodedImage image = new EncodedImage(ref);
try {
CloseableImage image = ref.get();

image.parseMetaData();
WritableMap sizes = Arguments.createMap();
sizes.putInt("width", image.getWidth());
sizes.putInt("height", image.getHeight());
Expand All @@ -106,14 +108,15 @@ protected void onNewResultImpl(
promise.reject(ERROR_GET_SIZE_FAILURE, e);
} finally {
CloseableReference.closeSafely(ref);
image.close();
}
} else {
promise.reject(ERROR_GET_SIZE_FAILURE);
}
}

@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.getFailureCause());
}
};
Expand Down