Search code examples
androideclipseandroid-intentandroid-image

Display captured image


I am trying to take a picture in from my camera and put it on my application as a thumbnail, it successfully lets me take a photo but will not go further than that, it only seems to output the image which is the logo of my application.

Here is the code:

public void breakfastPicture(){

    bPicButton = (Button)findViewById(R.id.bPicButton);
    bPicButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            File file = new File(Environment.getExternalStorageDirectory(),
                    "breakfast.jpg");
            Uri photoPath = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

            // start camera activity
            startActivityForResult(intent, TAKE_PICTURE);

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

     if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){

            Bundle extras = intent.getExtras();
            ImageView bThumbnail = (ImageView)findViewById(R.id.bThumbnail);

            Bitmap bBitMap = (Bitmap) extras.get("data");
            bThumbnail.setImageBitmap(bBitMap);

        }
 }

Solution

  • Instead of this:

    Bundle extras = intent.getExtras();
    Bitmap bBitMap = (Bitmap) extras.get("data");
    

    Try using:

    File file = new File(Environment.getExternalStorageDirectory(),"breakfast.jpg");
    ImageView bThumbnail = (ImageView)findViewById(R.id.bThumbnail);
    Bitmap bBitMap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
    
    
    bThumbnail.setImageBitmap(bBitMap);
    

    decodeSampleBitmapFromFile method:

     public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
        // BEST QUALITY MATCH
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
    
        // Calculate inSampleSize, Raw height and widht of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;
    
        if (height > reqHeight)
        {
            inSampleSize = Math.round((float)height/(float)reqHeight);
        }
        int expectedWidth = width/inSampleSize;
    
        if(expectedWidth > reqWidth)
        {
            inSampleSize = Math.round((float)width/(float)reqWidth);
        }
    
        options.inSampleSize = inSampleSize;
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
    
        return BitmapFactory.decodeFile(path, options);
    }