1

i have been working on wallpaper app for a while and it's almost done but the size of app is pretty big cause i use .png extension so currently i'm trying to load jpg via assets instead of png in res

i tried to implement this answer Images from Assets folder in a GridView i get an error while loading the imageadapter

  • 02-21 23:13:05.883: E/AndroidRuntime(17634): FATAL EXCEPTION: main
  • 02-21 23:13:05.883: E/AndroidRuntime(17634): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagieview05/com.example.imagieview05.MainActivity}: java.lang.NullPointerException

here is my code

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;

}

here is the original working code without Assets reference

 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



    };
};

thanks for help in advance

Community
  • 1
  • 1
Joseph27
  • 129
  • 3
  • 16

2 Answers2

0

mBitArray = mImageArray; its pointing mBitArray toward mImageArray witch is nothing, maybe?

i dont think it really matters if you use jpeg or png memory wise anyway, in the program its going to completely uncompress the jpeg anyway

JRowan
  • 6,824
  • 8
  • 40
  • 59
  • well i use about 13 pics the total size of apk is 8.5 mb in case of png and 2.5 mb in case of Jpg – Joseph27 Feb 22 '13 at 06:53
  • when i tried to debug mbitarray returns [null, null, null, null] – Joseph27 Feb 23 '13 at 17:18
  • before or after you put it in the new ImageAdapter because your method to load the images is right, i still think its mBitArray = mImageArray that should be mImageArray = mBitArray – JRowan Feb 23 '13 at 19:22
  • before i pass it to adapter i think thats the reason the app crash – Joseph27 Feb 23 '13 at 20:29
  • well im still a beginner, i never used gridview or baseadapter, sorry if i couldnt help – JRowan Feb 23 '13 at 22:18
  • thanks anyway :D i think i might just use ordinary png source maybe this code is too complex for me – Joseph27 Feb 23 '13 at 22:24
  • i was able to load the pictures from assets to my GRidview but i don't know how to get certain pic from my Bitmap array via position – Joseph27 Feb 26 '13 at 23:38
  • what about all the methods in your imageadapter – JRowan Feb 26 '13 at 23:49
  • i tried to return Bitmap from imageadapter via `codepublic Bitmap getBitMap(int position) throws IOException { AssetManager am = mContext.getAssets(); String[] list = am.list("Gallary"); BufferedInputStream buf = new BufferedInputStream(am.open(list[position])); Bitmap bitmap = BitmapFactory.decodeStream(buf); buf.close(); return bitmap; } ` – Joseph27 Feb 27 '13 at 06:43
  • as you can see i created Bitmap via assetmanager and Selected certain Position my AssetManager opens "Gallary" to list the images but it doesn't work unless i put them in The Root of Assets folder i don't know why it should loads from Gallary folder not the Root btw if i let the MBitarray load from The Root my Assetmanager works if i change my Mbitarray to load from Gallary folder my Asssetmanger won't woork any idea? – Joseph27 Feb 27 '13 at 07:07
  • btw thanks for ur support as my first app it means a lot for me – Joseph27 Feb 27 '13 at 07:08
  • put in the root/gallary, im guessing – JRowan Feb 27 '13 at 09:55
0

I wrote the code loader images from assets folder following the example 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;
}

Load *.jpg work, but *.png not work for ex.

IMG // work

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

        imageViewTest.setImageBitmap(bitmap);
    }
    else {

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

PNG // not work ( is error )

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

        imageViewTest.setImageBitmap(bitmap);
    }
    else {

        Log.e("ERROR", "error");
    }
xShoter26
  • 11
  • 1
  • 1