2015-01-28 8 views
1

장치가 이미지 형식 (특히 webP 형식)을 지원하는지 확인하는 방법이 있습니까? android doc (http://developer.android.com/guide/appendix/media-formats.html)에 따르면 webP는 4.0 이상의 장치에서 지원됩니다. enter image description here장치가 webP 이미지 형식을 지원하는지 확인하십시오.

그러나 4.0+ 기기 중 일부는 아직 webP를 지원하지 않습니다. (예 : Noxia XL-http://developer.nokia.com/community/wiki/Nokia_X_known_issues). 장치가 webP 이미지를 지원하는지 여부를 프로그래밍 방식으로 확인할 수있는 방법이 있습니까? 도움이 될만한 점이 없습니다. 감사합니다.

+0

작은 (× 1)을 넣어 그것을 디코딩 ... 결과가 null의 경우, 또는 메소드가 예외 WebP 형식은 지원되지 않습니다 – Selvin

+0

@Selvin 감사가 발생한다면, 시도 :

는 다음과 같이 구현 될 수 나는 이것을 시도 할 것이다. 그러나 이것을 달성 할 수있는 직접적인 방법이 있습니까? 안드로이드는 미디어 호환성을 확인하기 위해 inbuilt 함수/API를 제공합니까? –

+0

* 그러나 이것을 달성 할 수있는 간단한 방법이 있습니까? * 그런 식으로는 알지 못합니다. 시도해보십시오. https://gist.github.com/SelvinPL/d94b2b617519e0510a40 – Selvin

답변

4

내가 아는 한 여기에는 API가 없습니다. 그래서 해결책은 장치의 일부 웹 이미지를 디코딩하고 Bitmap을 반환하는지 확인하는 것입니다. 자산에 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; 
    } 
}