0

Android 에뮬레이터에 빈 화면이 표시되어 이미지가 계속 열립니다 (150,150 사진이있는 것으로 가정). 다른 말로하면, 내 150,150 엄지 손가락은 빈 화면 뒤에 있습니다Android에 JPEG 이미지가 표시되지 않습니다.

내 이미지가 150 * 150보다 큰 경우, 큰 비트 맵을 효율적으로로드하기 위해 아래 링크를 따라 갔지만 여전히 작동하지 않습니다.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

이들은 나는 또한 자세한 내용은 내 전체 imageAdapter 클래스를 추가 한 imageAdapter

public static 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(reqHeight + reqWidth == 0){ 

      return inSampleSize; 
     } 
     else if (height > reqHeight || width > reqWidth) { 

      // Calculate ratios of height and width to requested height and width 
      final int heightRatio = Math.round((float) height/(float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 

      // Choose the smallest ratio as inSampleSize value, this will guarantee 
      // a final image with both dimensions larger than or equal to the 
      // requested height and width. 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
     } 

     return inSampleSize; 
    } 

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

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

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 

내 코드입니다.

package com.example.first; 

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.util.Log; 

public class ImageAdapter extends BaseAdapter{ 

    private Context mContext; 
    public Integer[] mThumbIds = { 
      R.drawable.h18, R.drawable.h17, R.drawable.h16, R.drawable.h15, 



    }; 
    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 

     return mThumbIds[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public static 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(reqHeight + reqWidth == 0){ 

      return inSampleSize; 
     } 
     else if (height > reqHeight || width > reqWidth) { 

      // Calculate ratios of height and width to requested height and width 
      final int heightRatio = Math.round((float) height/(float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 

      // Choose the smallest ratio as inSampleSize value, this will guarantee 
      // a final image with both dimensions larger than or equal to the 
      // requested height and width. 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
     } 

     return inSampleSize; 
    } 

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

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

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageview; 
     if(convertView == null){ 
      imageview = new ImageView(mContext); 
      imageview.setScaleType(ImageView.ScaleType.FIT_XY); 
      imageview.setLayoutParams(new GridView.LayoutParams(150,150)); 
      imageview.setPadding(8, 8, 8, 8); 



     }else{ 
      imageview = (ImageView) convertView; 
     } 



     imageview.setImageBitmap(decodeSampledBitmapFromResource(null, mThumbIds[position], 150, 150)); 
     return imageview; 
    } 

    private Resources getResources() { 
     // TODO Auto-generated method stub 
     return null; 
    } 



} 

감사합니다.

+0

내가 안드로이드는 PNG와 JPEG –

+0

같은 문제를 지원하는 경우 매우 확실하지이기 때문에 당신이, 당신을 일 –

답변

0

Resources 대신 null으로 비트 맵 로더를 호출하고 있습니다.

트릭을 할해야

decodeSampledBitmapFromResource(mContext.getResources(), mThumbIds[position], 150, 150) 

으로

decodeSampledBitmapFromResource(null, mThumbIds[position], 150, 150) 

교체.

+0

.. JPEG 이미지가 최대 열립니다 ..하지만 상단에이 빈 화면이, PNG 이미지를 사용하려고 했 놀랍다 .. –

관련 문제