Skip to content

Commit

Permalink
fix(android): add on error call back for get image
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and zoomchan-cxj committed Jul 25, 2022
1 parent 23caa41 commit 17e1481
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Expand Up @@ -48,16 +48,18 @@ public void setDrawable(Drawable drawable) {
*
* @param rawData byte array raw data
*/
public void setData(byte[] rawData) {
public boolean setData(byte[] rawData) {
try {
mGifMovie = Movie.decodeByteArray(rawData, 0, rawData.length);
if (mGifMovie == null) {
mBitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
} else {
mBitmap = null;
}
return true;
} catch (OutOfMemoryError | Exception e) {
e.printStackTrace();
return false;
}
}

Expand All @@ -66,16 +68,17 @@ public void setData(byte[] rawData) {
*
* @param path file path of the image
*/
public void setData(File path) {
public boolean setData(File path) {
FileInputStream is = null;
try {
is = new FileInputStream(path);
byte[] rawData = new byte[is.available()];
int total = is.read(rawData);
LogUtils.d("HippyDrawable", "setData path: read total=" + total);
setData(rawData);
return setData(rawData);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (is != null) {
try {
Expand Down Expand Up @@ -111,17 +114,20 @@ public void setData(File path, boolean isGif) {
}
}

public void setDataForTarge28Assets(String assetsFile) {
public boolean setDataForTarge28Assets(String assetsFile) {
ImageDecoder.Source source;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
try {
source = ImageDecoder.createSource(ContextHolder.getAppContext().getAssets(),
assetsFile.substring("assets://".length()));
mBitmap = ImageDecoder.decodeBitmap(source);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return false;
}

/**
Expand All @@ -139,7 +145,7 @@ public void setData(Bitmap bmp) {
*
* @param source 不是原始数据,而是原始数据的来源:base64 / assets / file
*/
public void setData(String source) {
public boolean setData(String source) {
mSource = source;
if (mSource.startsWith("data:")) {
try {
Expand All @@ -150,31 +156,34 @@ public void setData(String source) {
String base64String = mSource.substring(base64Index);
byte[] decode = Base64.decode(base64String, Base64.DEFAULT);
if (decode != null) {
setData(decode);
return setData(decode);
}
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else if (mSource.startsWith("file://")) {
// local file image
String filePath = mSource.substring("file://".length());
setData(new File(filePath));
return setData(new File(filePath));
} else if (mSource.startsWith("assets://")) {
InputStream is = null;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
setDataForTarge28Assets(mSource);
return setDataForTarge28Assets(mSource);
} else {
is = ContextHolder.getAppContext().getAssets()
.open(mSource.substring("assets://".length()));
byte[] rawData = new byte[is.available()];
int total = is.read(rawData);
LogUtils.d("HippyDrawable", "setData source: read total=" + total);
setData(rawData);
return setData(rawData);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (is != null) {
try {
Expand All @@ -185,6 +194,7 @@ public void setData(String source) {
}
}
}
return false;
}

/**
Expand Down
Expand Up @@ -45,7 +45,9 @@ public HippyDrawable getImage(String source, Object param) {
}
}
HippyDrawable drawable = new HippyDrawable();
drawable.setData(source);
if (!drawable.setData(source)) {
return null;
}
if (canCacheImage) {
mWeakImageCache.put(imageCacheCode, new WeakReference<>(drawable));
}
Expand Down

0 comments on commit 17e1481

Please sign in to comment.