2013-11-23 2 views
4

나는 안드로이드 초보자이며 문제가 있습니다. 자산 폴더에서 이미지를로드해야하는 응용 프로그램을 만들고 있습니다. 이미지는 투명 배경이있는 png 형식입니다. 나는 목적을 위해 내가 가진 문제는이 구글이 자신의 웹 사이트에 제안하는 방식 (이 같은) =>를Nexus 7, Android 4.4 및 PNG 이미지

public Bitmap getBitmap(Activity activity, String fileName) {   
      try 
      { 
       int reqWidth =getCalculatedWidth(); 
       int reqHeight=getCalculatedHeight(); 
       AssetManager asManager =activity.getAssets(); 
       InputStream istr = asManager.open(fileName); 
       // First decode with inJustDecodeBounds=true to check dimensions 
       final BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 

       BitmapFactory.decodeStream(istr, null, options); 

       options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

       // Decode bitmap with inSampleSize set 
       options.inJustDecodeBounds = false; 
       Bitmap bmp =BitmapFactory.decodeStream(istr,null, options); 
       Toast.makeText(activity, fileName+" was successfuly loaded", Toast.LENGTH_SHORT).show(); 
       return bmp; 


      } 
      catch (IOException ex) 
      { 
       Toast.makeText(activity, ex.getMessage(), Toast.LENGTH_LONG).show(); 
       return null; 
      } 

     } 

private int calculateInSampleSize(
      BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      // Calculate the largest inSampleSize value that is a power of 2 and 
      // keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) > reqHeight 
        && (halfWidth/inSampleSize) > reqWidth) { 
       inSampleSize *= 2; 
      } 

     } 

     return inSampleSize; 
    } 

BitmapFactory를 사용하고 있습니다. 모든 것이 예상대로 진행되었지만 어제 Android 4.4를 설치했으며 앱을 시작할 때 이미지가로드되지 않았습니다. 연구를 시도했지만 내 문제와 관련이 없으며 PNG 이미지를 전혀로드하지 않습니다. 나는 그들을 GIF로 변환하고 이미지가로드되었지만 대신 흰색 배경으로 투명하게 보였다. 이 게시물을 발견 : https://code.google.com/p/android/issues/detail?id=62016, 그리고 나를 위해 workaround 작동하지만 GIF ​​이미지를 사용하고 싶지 않아, 나는 png 싶어요. GIF 품질이 만족스럽지 않습니다.

Toast "파일이 성공적으로로드되었습니다"라는 메시지가 표시되므로 예외가 발생하지 않습니다.

무엇이 문제 일 수 있으며 이는 나에게만 일어나는 일입니까?

내가 테스트하는 기기 : Nexus 7 2012 (Wi-Fi) 모바일이 아닙니다. 4.3이 있으며 어제는 4.4로 업데이트됩니다 (일반 시스템 업데이트로).

다른 장치에서는 이전과 같이 잘 작동하므로 에뮬레이터에서도 정상적으로 작동합니다. (테스트 한 다른 장치에는 4.4가 없지만 이전 버전은 없습니다.) 내 넥서스 7에만 4.4가 있으며 작동하지 않는 유일한 곳입니다. (4.3에서 작동했습니다.)

도움을 주시면 감사하겠습니다. 감사합니다.

답변

4

같은 상황에서 같은 문제가있었습니다. 나는 그렇지 BitmapFactory.decodeStream 다시 inputStream을를 사용하는 반환 inputStream.reset을 (호출 문제)를 해결했다. 다음 안드로이드 킷캣 4.4에서 문제에 직면하고있다

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 

BitmapFactory.decodeStream(istr, null, options); 

try { 
    istr.reset(); 
} catch (IOException e) { 
    Log.e(TAG, e.getMessage()); 
} 

options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
Bitmap bmp = BitmapFactory.decodeStream(istr,null, options); 
+0

솔루션이 너무 단순하다고 생각할 수 없습니다. 나는 다른 해결책을 다시 구현하기 위해 며칠을 보냈지 만 이것만큼 좋지는 않았다. 고마워요. – aleksandaril

+0

누구가 생각했을까요? 4.4 (오랫동안 작동하지 않고 디코딩하지 않고 디코딩하는 것을 의미합니다.) –

+0

istr.reset()은 IO 예외를 발생시킵니다. – Slartibartfast

0

다른 사람들은 도움이 될 것입니다

비트 맵 B = BitmapFactory.decodeStream (새 BufferedInputStream을 (이다)); imageView.setImageBitmap (b);

4.4로 업데이트 한 후 Nexus 시리즈에서이 문제가 발생했습니다.

관련 문제