Skip to content

Commit

Permalink
fix(android): image module get size support return bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Oct 15, 2024
1 parent 53c8a09 commit 151a060
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.tencent.mtt.hippy.modules.nativemodules.image;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import androidx.annotation.NonNull;
Expand All @@ -25,6 +26,7 @@
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.mtt.hippy.runtime.builtins.JSObject;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.vfs.ResourceDataHolder;
import com.tencent.vfs.VfsManager;
import com.tencent.vfs.VfsManager.FetchResourceCallback;
Expand All @@ -33,6 +35,7 @@
@HippyNativeModule(name = "ImageLoaderModule")
public class ImageLoaderModule extends HippyNativeModuleBase {

private final String TAG = "ImageLoaderModule";
private final VfsManager mVfsManager;
private static final String ERROR_KEY_MESSAGE = "message";

Expand Down Expand Up @@ -64,6 +67,41 @@ private HashMap<String, String> generateRequestParams() {
return requestParams;
}

private void onFetchFailed(final String url, final Promise promise, @NonNull final ResourceDataHolder dataHolder) {
String message =
dataHolder.errorMessage != null ? dataHolder.errorMessage : "";
String errorMsg = "Fetch image failed, url=" + url + ", msg=" + message;
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, errorMsg);
promise.reject(jsObject);
}

private void handleFetchResult(final String url, final Promise promise, @NonNull final ResourceDataHolder dataHolder) {
byte[] bytes = dataHolder.getBytes();
Bitmap bitmap = dataHolder.bitmap;
LogUtils.d(TAG, "handleFetchResult: url " + url + ", result " + dataHolder.resultCode);
if (dataHolder.resultCode == ResourceDataHolder.RESOURCE_LOAD_SUCCESS_CODE) {
if (bitmap != null && !bitmap.isRecycled()) {
LogUtils.d(TAG, "handleFetchResult: url " + url
+ ", bitmap width " + bitmap.getWidth() + ", bitmap height " + bitmap.getHeight());
JSObject jsObject = new JSObject();
jsObject.set("width", bitmap.getWidth());
jsObject.set("height", bitmap.getHeight());
promise.resolve(jsObject);
} else if (bytes != null && bytes.length > 0) {
decodeImageData(url, bytes, promise);
} else {
if (TextUtils.isEmpty(dataHolder.errorMessage)) {
dataHolder.errorMessage = "Invalid image data or bitmap!";
}
onFetchFailed(url, promise, dataHolder);
}
} else {
onFetchFailed(url, promise, dataHolder);
}
dataHolder.recycle();
}

@HippyMethod(name = "getSize")
public void getSize(final String url, final Promise promise) {
if (TextUtils.isEmpty(url)) {
Expand All @@ -76,20 +114,7 @@ public void getSize(final String url, final Promise promise) {
new FetchResourceCallback() {
@Override
public void onFetchCompleted(@NonNull final ResourceDataHolder dataHolder) {
byte[] bytes = dataHolder.getBytes();
if (dataHolder.resultCode
!= ResourceDataHolder.RESOURCE_LOAD_SUCCESS_CODE || bytes == null
|| bytes.length <= 0) {
String message =
dataHolder.errorMessage != null ? dataHolder.errorMessage : "";
String errorMsg = "Fetch image failed, url=" + url + ", msg=" + message;
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, errorMsg);
promise.reject(jsObject);
} else {
decodeImageData(url, bytes, promise);
}
dataHolder.recycle();
handleFetchResult(url, promise, dataHolder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void draw(@NonNull Canvas canvas) {
drawGif(canvas, mImageHolder.getGifMovie());
} else {
Bitmap bitmap = mImageHolder.getBitmap();
if (bitmap != null) {
if (bitmap != null && !bitmap.isRecycled()) {
drawBitmap(canvas, bitmap);
}
}
Expand Down

0 comments on commit 151a060

Please sign in to comment.