AsyncTask all i get is a blank/black texture screen

AsyncTask all i get is a blank/black texture screen

am using Open GL ES 2.0 to perform certain effects on bitmaps, now if i load a bitmap directly on UI thread it takes alot of time, hence im using AsyncTask....

Problem:-

When i use AsyncTask,all i get is a blank/black texture screen,without asynctask it display the bitmap after 7-8 seconds depending on the size.

Following is the code:-

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
public class TinypostFilters extends Activity implements GLSurfaceView.Renderer {  
 
                private Uri myuri;  
                int dw;  
                int dh;  
 
                private GLSurfaceView mEffectView;  
                private int[] mTextures = new int[2];  
                private EffectContext mEffectContext;  
                private Effect mEffect;  
                private TextureRenderer mTexRenderer = new TextureRenderer();  
                private int mImageWidth;  
                private int mImageHeight;  
                private boolean mInitialized = false;  
 
 
                @Override 
                public void onCreate(Bundle savedInstanceState) {  
                    super.onCreate(savedInstanceState);  
                    setContentView(R.layout.activity_tinypost_filters);  
 
                    /* 
                     * Initialize renderer and set it to only render when explicitly 
                     * requested with the RENDERMODE_WHEN_DIRTY option. 
                     */ 
 
                    mEffectView = (GLSurfaceView) findViewById(R.id.effectsview);  
                    mEffectView.setEGLContextClientVersion(2);  
                    mEffectView.setRenderer(this);  
                    mEffectView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);  
                    mCurrentEffect = R.id.none;  
 
                }  
 
 
 
    @Override 
    public void onDrawFrame(GL10 gl) {  
        if (!mInitialized) {  
            // Only need to do this once  
            mEffectContext = EffectContext.createWithCurrentGlContext();  
            mTexRenderer.init();  
 
            loadTextures();  
 
            mInitialized = true;  
        }  
        if (mCurrentEffect != R.id.none) {  
            // if an effect is chosen initialize it and apply it to the texture  
            initEffect();  
            //applyEffect();  
        }  
        renderResult();  
    }  
 
                @Override 
                public void onSurfaceChanged(GL10 gl, int width, int height) {  
                    if (mTexRenderer != null) {  
                        mTexRenderer.updateViewSize(width, height);  
                    }  
                }  
 
                @Override 
                public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
                }  
 
 
           private void loadTextures() {  
                    // Generate textures  
                    GLES20.glGenTextures(2, mTextures, 0);  
 
                    // Load input bitmap  
 
                    try {  
                    //load Bitmap from previous activity  
    myuri = Uri.parse(getIntent().getStringExtra("uri"));  
 
        Bitmap bitmap=new BitmapWorkerTask().execute(myuri).get();  
                    mImageWidth = bitmap.getWidth();  
                    mImageHeight = bitmap.getHeight();  
                    mTexRenderer.updateTextureSize(mImageWidth, mImageHeight);  
 
                    // Upload to texture  
                    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);  
                    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);  
 
                    // Set texture parameters  
                    GLToolbox.initTexParams();  
                    } catch (Exception e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
 
                }  
 
 
           private void renderResult() {  
        if (mCurrentEffect != R.id.none) {  
            // if no effect is chosen, just render the original bitmap  
            mTexRenderer.renderTexture(mTextures[1]);  
        } else {  
            // render the result of applyEffect()  
            mTexRenderer.renderTexture(mTextures[0]);  
        }  
    }  
 
 
 
 
 
 
 //this method decodes the bitmap  
                public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth,  
                        int reqHeight) {  
 
                    Display currentDisplay = getWindowManager().getDefaultDisplay();  
                    Point size = new Point();  
                    currentDisplay.getSize(size);  
 
                    dw = size.x;  
                    dh = size.y;  
 
                    // Load up the image's dimensions not the image itself  
                    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
                    bmpFactoryOptions.inJustDecodeBounds = true;  
                    Bitmap bmp = null;  
                    try {  
                        bmp = BitmapFactory.decodeStream(getContentResolver()  
                                .openInputStream(uri), null, bmpFactoryOptions);  
                    } catch (Exception e) {  
 
                    }  
                    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth  
                            / (float) reqWidth);  
                    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight  
                            / (float) reqHeight);  
 
                    if (heightRatio > 1 && widthRatio > 1) {  
                        if (heightRatio > widthRatio) {  
                            bmpFactoryOptions.inSampleSize = heightRatio;  
                        } else {  
                            bmpFactoryOptions.inSampleSize = widthRatio;  
                        }  
 
                        bmpFactoryOptions.inJustDecodeBounds = false;  
                        try {  
                            bmp = BitmapFactory.decodeStream(getContentResolver()  
                                    .openInputStream(uri), null, bmpFactoryOptions);  
 
                        } catch (Exception e) {  
 
                        }  
 
                    }  
                    return bmp;  
 
                }  
 
 
 
                class BitmapWorkerTask extends AsyncTask<Uri, Void, Bitmap> {  
 
                    Uri uri;  
 
                    @Override 
                    protected Bitmap doInBackground(Uri... imageuri) {  
 
                        uri = imageuri[0];  
                        return decodeSampledBitmapFromResource(uri, dw, dh);  
                    }  
 
                }  
 
            } 
public class TinypostFilters extends Activity implements GLSurfaceView.Renderer {

                private Uri myuri;
                int dw;
                int dh;

                private GLSurfaceView mEffectView;
                private int[] mTextures = new int[2];
                private EffectContext mEffectContext;
                private Effect mEffect;
                private TextureRenderer mTexRenderer = new TextureRenderer();
                private int mImageWidth;
                private int mImageHeight;
                private boolean mInitialized = false;


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

                    /*
                     * Initialize renderer and set it to only render when explicitly
                     * requested with the RENDERMODE_WHEN_DIRTY option.
                     */

                    mEffectView = (GLSurfaceView) findViewById(R.id.effectsview);
                    mEffectView.setEGLContextClientVersion(2);
                    mEffectView.setRenderer(this);
                    mEffectView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
                    mCurrentEffect = R.id.none;

                }

 

    @Override
    public void onDrawFrame(GL10 gl) {
        if (!mInitialized) {
            // Only need to do this once
            mEffectContext = EffectContext.createWithCurrentGlContext();
            mTexRenderer.init();

            loadTextures();

            mInitialized = true;
        }
        if (mCurrentEffect != R.id.none) {
            // if an effect is chosen initialize it and apply it to the texture
            initEffect();
            //applyEffect();
        }
        renderResult();
    }

                @Override
                public void onSurfaceChanged(GL10 gl, int width, int height) {
                    if (mTexRenderer != null) {
                        mTexRenderer.updateViewSize(width, height);
                    }
                }

                @Override
                public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                }


           private void loadTextures() {
                    // Generate textures
                    GLES20.glGenTextures(2, mTextures, 0);

                    // Load input bitmap

                    try {
                    //load Bitmap from previous activity
    myuri = Uri.parse(getIntent().getStringExtra("uri"));

        Bitmap bitmap=new BitmapWorkerTask().execute(myuri).get();
                    mImageWidth = bitmap.getWidth();
                    mImageHeight = bitmap.getHeight();
                    mTexRenderer.updateTextureSize(mImageWidth, mImageHeight);

                    // Upload to texture
                    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
                    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

                    // Set texture parameters
                    GLToolbox.initTexParams();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }


           private void renderResult() {
        if (mCurrentEffect != R.id.none) {
            // if no effect is chosen, just render the original bitmap
            mTexRenderer.renderTexture(mTextures[1]);
        } else {
            // render the result of applyEffect()
            mTexRenderer.renderTexture(mTextures[0]);
        }
    }

 

 


 //this method decodes the bitmap
                public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth,
                        int reqHeight) {

                    Display currentDisplay = getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    currentDisplay.getSize(size);

                    dw = size.x;
                    dh = size.y;

                    // Load up the image's dimensions not the image itself
                    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                    bmpFactoryOptions.inJustDecodeBounds = true;
                    Bitmap bmp = null;
                    try {
                        bmp = BitmapFactory.decodeStream(getContentResolver()
                                .openInputStream(uri), null, bmpFactoryOptions);
                    } catch (Exception e) {

                    }
                    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
                            / (float) reqWidth);
                    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
                            / (float) reqHeight);

                    if (heightRatio > 1 && widthRatio > 1) {
                        if (heightRatio > widthRatio) {
                            bmpFactoryOptions.inSampleSize = heightRatio;
                        } else {
                            bmpFactoryOptions.inSampleSize = widthRatio;
                        }

                        bmpFactoryOptions.inJustDecodeBounds = false;
                        try {
                            bmp = BitmapFactory.decodeStream(getContentResolver()
                                    .openInputStream(uri), null, bmpFactoryOptions);

                        } catch (Exception e) {

                        }

                    }
                    return bmp;

                }

 

                class BitmapWorkerTask extends AsyncTask<Uri, Void, Bitmap> {

                    Uri uri;

                    @Override
                    protected Bitmap doInBackground(Uri... imageuri) {

                        uri = imageuri[0];
                        return decodeSampledBitmapFromResource(uri, dw, dh);
                    }

                }

            }I've tried refactoring here and there,but without any success...any help is appreciated,thanks!

[1] [2] [3] Next

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright