NullPointerException
NullPointerException

Reputation: 37577

How to create a bitmap with RGB_565?

actually i'm opening png files from assets folder with this code:

public static Bitmap loadImage( String imageName ){
    if( imageName.charAt(0) == '/' ) {
        imageName = imageName.substring(1);
    }
    imageName = imageName + ".png";
    Bitmap image = BitmapFactory.decodeStream(getResourceAsStream(imageName));
    return image;
}
public static InputStream getResourceAsStream( String resourceName ) {
    if( resourceName.charAt(0) == '/' ) {
        resourceName = resourceName.substring(1);
    }

    InputStream is = null;
    try {
        is = context.getAssets().open( resourceName );
    } catch (IOException e) {e.printStackTrace();}
    return is;
}

This code opens the bitmaps with full cuality and it takes a lot of time to open it. I will try to use RGB_565 to speed up the opening of the bitmap.

What should i change into my code to open the bitmap with RGB_565? As you can see, i dont know the width and the height of the image.

Also any sugerences to speed up the opening of the bitmap will be welcome

Thanks

Upvotes: 2

Views: 5092

Answers (2)

Thilina Anuradh
Thilina Anuradh

Reputation: 211

This code worked for me

    AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open(strName);
    } catch (IOException e) {
        e.printStackTrace();
    }

    BitmapFactory.Options bitmapLoadingOptions = new BitmapFactory.Options();
    bitmapLoadingOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap bitmap = BitmapFactory.decodeStream(istr,null,bitmapLoadingOptions);


    return bitmap;

Upvotes: 0

dbryson
dbryson

Reputation: 6127

Add BitmapFactory.Options to to decodeStream() call:

BitmapFactory.Options bitmapLoadingOptions = new BitmapFactory.Options();
bitmapLoadingOptions.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeStream(instream,null,bitmapLoadingOptions);

As for how to speed up loading the image? Not sure what else you can do except maybe reduce the size of the image if that's possible.

Upvotes: 8

Related Questions