views:

68

answers:

2

I am trying to display an bitmap I created in and android program, but all the tutorials I find involve either Drawables or XML in order to display them. Can someone show me the steps needed to display an Bitmap in code?

This is not the entirety of the code, this is just the majority, the rest is related to getting the camera working.

class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Paint mPaint;
private Drawable bitmap;
private Bitmap currentprev;
private ImageView mImageView;

private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback() 
{
        public void onPreviewFrame( byte[] data, Camera Cam ) {
                Log.d("CombineTestActivity", "Preview registered");
                Log.d("CombineTestActivity", "Data length = " 
                        + data.length );
               // currentprev = BitmapFactory.decodeByteArray( data, 0, 
               //       data.length );
               currentprev = BitmapFactory.decodeResource( getResources(),
                   R.drawable.creature00 );
               if( currentprev == null )
                   Log.d("CombineTestActivity", "currentprev is null" );
               if( mImageView == null )
                   Log.d("CombineTestActivity", "mImageView is null" );
        //Code fails here, gives null pointer exception
                mImageView.setImageBitmap( currentprev );
                Log.d("CombineTestActivity", "Preview Finished" );
        }
};

private OnTouchListener mCorkyListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {
                Log.d("CombineTestActivity", "touch registered" );
                mCamera.takePicture( null, null, mPicCallback );
                return false;
        }
};

private Camera.PictureCallback mPicCallback = new Camera.PictureCallback() {
        public void onPictureTaken( byte[] data, Camera mCamera ) {

                Log.d("CombineTestActivity", "picture method run" );
        }
};

Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mImageView = (ImageView)findViewById(R.id.imageview);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mPaint = new Paint();
    bitmap = context.getResources().getDrawable(
        R.drawable.creature00);
    if( mImageView == null )
        Log.d("CombineTestActivity", "mImageView is null" );

this.setOnTouchListener( mCorkyListener );

}
+1  A: 
Bitmap bm; // some bitmap you have loaded.
ImageView iv; // the image view you want to display the bitmap in

iv.setImageBitmap(bm);
BrennaSoft
+1  A: 

You need to use the BitmapFactory to create the Bitmap and load it into the ImageView. Like this:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
ImageView view = (ImageView)findViewById(R.id.my_image_view);
view.setImageBitmap(image);
CaseyB
Can you show me some basic example code? I'm new to android and not sure what all is needed and how it all goes together
RyoxSinfar
I edited my answer to add an example.
CaseyB
I have tried making an image view now with a constructor passing in the context, as well as doing it the way you wrote with an XML defined View but each time the ImageView object is coming out as NULL as soon as the initializing statement is finished
RyoxSinfar
Can you post the code that you're using?
CaseyB
Just updated, for the main.xml I'm just setting the width, height, and ID. and I am baseing all that on how its done in the tutorials.
RyoxSinfar