2013-02-21 80 views
1

我一直在做壁纸应用一段时间,现在差不多完成了,但应用的尺寸很大,因为我使用.png扩展名,所以目前我试图通过资源加载jpg而不是png RES从资产中加载图片

我试图执行这个答案 Images from Assets folder in a GridView 在加载imageadapter

  • 02-21 23我得到一个错误:13:05.883:E/AndroidRuntime(17634):致命异常:主要
  • 02-21 23:13:05.883:E/AndroidRuntime(17634):java.lang.Run timeException:无法启动活动ComponentInfo {com.example.imagieview05/com.example.imagieview05.MainActivity}:显示java.lang.NullPointerException

这里是我的代码

public class MainActivity extends Activity { 
private GridView mGridView; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mGridView = (GridView) findViewById(R.id.GridView1); 

    Bitmap[] mBitArray = new Bitmap[4]; 
    try { 
    mBitArray[0] = getBitmapFromAssets("g1p2.jpg"); 
    mBitArray[1] = getBitmapFromAssets("g1p1.jpg"); 
    mBitArray[2] = getBitmapFromAssets("g1p3.jpg"); 
    mBitArray[3] = getBitmapFromAssets("g1p4.jpg"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


    mGridView.setAdapter(new ImageAdapter(this ,mBitArray)); 

} 
public Bitmap getBitmapFromAssets (String filename) throws IOException{ 
    AssetManager assetManager = getAssets(); 
    InputStream istr = assetManager.open(filename); 
    Bitmap bitmap = BitmapFactory.decodeStream(istr); 
    return bitmap; 
} 




    public class ImageAdapter extends BaseAdapter{ 


private Context mContext; 
private Bitmap[] mImageArray; 


public GallaryAdapter(Context c, Bitmap[] mBitArray) { 
    c = mContext; 
    mBitArray = mImageArray; 
} 

public int getCount() { 
    return mImageArray.length; 
} 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return mImageArray[position]; 
} 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imgView = new ImageView(mContext); 
    imgView.setImageBitmap(mImageArray[position]); 
    //put black borders around the image 
    imgView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imgView.setLayoutParams(new GridView.LayoutParams(120, 120)); 
    return imgView; 

} 

原来这里是工作没有资产代码中引用

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    GridView gridView = (GridView) findViewById(R.id.GridView1); 
    gridView.setAdapter(new ImageAdapter(this)); 


public class ImageAdapter extends BaseAdapter { 

private Context mContext; 


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

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

    public Object getItem(int position) { 
     return null; 
    } 

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

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.g1p1, R.drawable.g1p2, 
      R.drawable.g1p3, R.drawable.g1p4, 
      R.drawable.g1p5, R.drawable.g1p6, 
      R.drawable.g1p22, R.drawable.g1p33, 
      R.drawable.g1p44, R.drawable.g1p55, 
      R.drawable.g1p5, R.drawable.g1p6, 
      R.drawable.g1p22, R.drawable.g1p33, 
      R.drawable.g1p44, R.drawable.g1p55 



    }; 
}; 

感谢您的帮助提前

回答

0

mBitArray = mImageAr射线;它指向mImageArray巫婆的mBitArray是没有,也许?

我不认为如果你使用JPEG或PNG内存明智的,无论如何,在节目中真正重要的去反正完全解压缩JPEG

+0

以及我使用约13图片APK的总大小是PNG和2.5的情况下,8.5 MB mb的情况下Jpg – Joseph27 2013-02-22 06:53:56

+0

正确的apk大小我想过 – JRowan 2013-02-22 16:33:06

+0

当我试图调试mbitarray返回[null,null,null,null] – Joseph27 2013-02-23 17:18:00

0

我写的代码从资产文件夹下面的例子code

装载机图片
private Bitmap decodeStreamFromAssets(String path, int reqWidth, int reqHeight) { 

    InputStream ims = null; 

    try { 

     ims = getAssets().open(path); 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(ims, null, options); 

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

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeStream(ims, null, options); 
    } 
    catch (IOException e) { 

     e.printStackTrace(); 
    } 
    finally { 

     if (ims != null) { 
      try { 

       ims.close(); 
      } 
      catch (IOException e) { 

       e.printStackTrace(); 
      } 
     } 
    } 

    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; 
} 

加载* .jpg工作,但* .png不适用于前。

IMG //工作

Bitmap bitmap = decodeStreamFromAssets("test.jpg", 64, 64); 
    if(bitmap1 != null){ 

     imageViewTest.setImageBitmap(bitmap); 
    } 
    else { 

     Log.e("ERROR", "error"); 
    } 

PNG //不起作用(是错误)

Bitmap bitmap = decodeStreamFromAssets("test.png", 64, 64); 
    if(bitmap1 != null){ 

     imageViewTest.setImageBitmap(bitmap); 
    } 
    else { 

     Log.e("ERROR", "error"); 
    }