Search code examples
androidandroid-layoutcanvascameraandroid-gallery

Why i am not able to take image from camera but can able to get image from Gallery in android?


I am using this code to fetch Image from gallery:

fromGalleryButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    canvasPictureDialog.dismiss();
                    Intent intent = new Intent(); 
                    intent.setType("image/*"); 
                    intent.setAction(Intent.ACTION_GET_CONTENT);// 
                    //startActivity(intent);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
                    //finish();
                }
            });

And to take image from camera i use this code:

private static final int TAKE_PHOTO_CODE = 1;  
private void takePhoto(){   
    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );    
    startActivityForResult(intent, TAKE_PHOTO_CODE); 
}

private File getTempFile(Context context){   
    //it will return /sdcard/image.tmp   
    //final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
    final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName());

    if(!path.exists()){     
        path.mkdir();   
    }   
    return new File(path, "image.tmp"); 
}

And common code for onActivityResult is:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data);     
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {             
        Uri contentUri = data.getData();          
        String[] proj = { MediaStore.Images.Media.DATA };         
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
        cursor.moveToFirst();         
        imagePath = cursor.getString(column_index);           
        //Bitmap croppedImage = BitmapFactory.decodeFile(imagePath);
        tempBitmap = BitmapFactory.decodeFile(imagePath);
        photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
    } 
    if(resultCode == RESULT_OK && requestCode==TAKE_PHOTO_CODE){
         final File file = getTempFile(this);         
         try {           

             tempBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
             photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
             // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)         
        } catch (FileNotFoundException e) {           
            e.printStackTrace();         
        } catch (IOException e) {           
            e.printStackTrace();         
        } 

    }

}

Now with using this code i am drawing the image on canvas:

 if(!(imagePath==null))
    {
        //tempBitmap = BitmapFactory.decodeFile(imagePath);
        //photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
        canvas.drawBitmap (photoBitmap,0,  0, null);
    }

But with that use, I am able to get Image from gallery but not from the camera. Why ?? Whats wrong in my code ?? Thanks.


Solution

  • Well, I have solve this problem for my own way. There is a logical mistake. I have taken the if condition on imagePath variable which can not works for the taking image from camera. I have taken one boolean Variable and set it for Taking Image from Camera and it works for me. Anyway, Thanks for the Comments and help. Thanks.