0

Android模擬器顯示空白屏幕,圖像仍然打開(假設有150,150張照片)。換句話說,我的150,150大拇指在空白屏幕後面Android未顯示任何JPEG圖像

我的圖像大於150 * 150,我按照下面的鏈接有效地加載大的位圖,但它仍然不起作用。

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

這些是我在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); 
    } 

代碼,我也加入了我的全imageAdapter類以獲取更多信息。

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圖像,因爲我不太確定如果android支持JPEG – 2013-04-29 18:36:04

+0

與PNG相同的問題,JPEG圖像在那裏並且打開了..但是上面有這個空白屏幕.. – 2013-04-29 18:45:21

回答

0

您在使用null而不是您的Resources對象調用您的位圖加載器。

更換

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

隨着

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

應該做的伎倆。

+0

它的工作,你真是太神奇了.. – 2013-04-29 20:44:21