Android 检查设备是否支持webP图像格式
有没有办法检查设备是否支持图像格式(Android 检查设备是否支持webP图像格式,android,android-image,webp,Android,Android Image,Webp,有没有办法检查设备是否支持图像格式(webPformat)。根据android doc(),webP在4.0+设备中受支持。 但一些4.0+设备还不支持webP。(适用于ex,Noxia XL-)。 是否有任何方法以编程方式检查设备是否支持webP映像。如有任何帮助,将不胜感激。谢谢 据我所知,这方面没有API。所以,解决方案是尝试在设备上解码一些webp图像,并检查它是否返回位图 这可以这样实现: import android.graphics.Bitmap; import android.
webP
format)。根据android doc(),webP在4.0+设备中受支持。
但一些4.0+设备还不支持webP。(适用于ex,Noxia XL-)。
是否有任何方法以编程方式检查设备是否支持webP映像。如有任何帮助,将不胜感激。谢谢 据我所知,这方面没有API。所以,解决方案是尝试在设备上解码一些webp图像,并检查它是否返回位图 这可以这样实现:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class WebPUtils {
//states
private static final int NOT_INITIALIZED = -1;
private static final int SUPPORTED = 1;
private static final int NOT_SUPPORTED = 0;
//why not boolean? we need more states for result caching
private static int isWebPSupportedCache = NOT_INITIALIZED;
public static boolean isWebPSupported() {
// did we already try to check?
if (isWebPSupportedCache == NOT_INITIALIZED) {
//no - trying to decode
//webp 1x1 transparent pixel with lossless
final byte[] webp1x1 = new byte[]{
0x52, 0x49, 0x46, 0x46, 0x1A, 0x00, 0x00, 0x00,
0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4C,
0x0D, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x00,
0x10, 0x07, 0x10, 0x11, 0x11, (byte) 0x88, (byte) 0x88, (byte) 0xFE,
0x07, 0x00
};
try {
final Bitmap bitmap = BitmapFactory.decodeByteArray(webp1x1, 0, webp1x1.length);
if (bitmap != null) {
//webp supported
isWebPSupportedCache = SUPPORTED;
//don't forget to recycle!
bitmap.recycle();
} else {
//bitmap is null = not supported
isWebPSupportedCache = NOT_SUPPORTED;
}
} catch (Exception ex) {
//we got some exception = not supported
isWebPSupportedCache = NOT_SUPPORTED;
ex.printStackTrace();
}
}
return isWebPSupportedCache == SUPPORTED;
}
}
据我所知,这方面没有API。所以,解决方案是尝试在设备上解码一些webp图像,并检查它是否返回位图 这可以这样实现:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class WebPUtils {
//states
private static final int NOT_INITIALIZED = -1;
private static final int SUPPORTED = 1;
private static final int NOT_SUPPORTED = 0;
//why not boolean? we need more states for result caching
private static int isWebPSupportedCache = NOT_INITIALIZED;
public static boolean isWebPSupported() {
// did we already try to check?
if (isWebPSupportedCache == NOT_INITIALIZED) {
//no - trying to decode
//webp 1x1 transparent pixel with lossless
final byte[] webp1x1 = new byte[]{
0x52, 0x49, 0x46, 0x46, 0x1A, 0x00, 0x00, 0x00,
0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4C,
0x0D, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x00,
0x10, 0x07, 0x10, 0x11, 0x11, (byte) 0x88, (byte) 0x88, (byte) 0xFE,
0x07, 0x00
};
try {
final Bitmap bitmap = BitmapFactory.decodeByteArray(webp1x1, 0, webp1x1.length);
if (bitmap != null) {
//webp supported
isWebPSupportedCache = SUPPORTED;
//don't forget to recycle!
bitmap.recycle();
} else {
//bitmap is null = not supported
isWebPSupportedCache = NOT_SUPPORTED;
}
} catch (Exception ex) {
//we got some exception = not supported
isWebPSupportedCache = NOT_SUPPORTED;
ex.printStackTrace();
}
}
return isWebPSupportedCache == SUPPORTED;
}
}
将小型(1x1)webp放入资产并尝试解码。。。若结果为null或方法引发异常,则webp不是supported@Selvin谢谢,我试试这个。但有没有直接的方法来实现这一点?我的意思是,android是否提供了任何内置的函数/api来检查媒体兼容性?但有没有直接的方法来实现这一点?我不知道这样。。。试试看:它起作用了。谢谢你能把你的评论作为答案贴出来吗?这样我就可以接受答案了。。。最好使用新版本。。。我忘了回收位图。。。此外,Base64在API>7上受支持。。。这个版本应该适用于所有android。。。我确实找到了更小的1x1 webp图像:)将小(1x1)webp放入资源中并尝试解码它。。。若结果为null或方法引发异常,则webp不是supported@Selvin谢谢,我试试这个。但有没有直接的方法来实现这一点?我的意思是,android是否提供了任何内置的函数/api来检查媒体兼容性?但有没有直接的方法来实现这一点?我不知道这样。。。试试看:它起作用了。谢谢你能把你的评论作为答案贴出来吗?这样我就可以接受答案了。。。最好使用新版本。。。我忘了回收位图。。。此外,Base64在API>7上受支持。。。这个版本应该适用于所有android。。。我发现了更小的1x1 webp图像:)