0

I am kind of new to android app development and trying to make a simple application which,

  1. Uses the system camera application to take picture by passing camera intent.
  2. Stores the image in the public folder in file system.
  3. Then takes the image from its absolute path convert it into bitmap and display it in image view by performing some scale down on it.

Here is my code....

static final int REQUEST_IMAGE_CAPTURE = 1;
String mCurrentPhotoPath;
Button captureImageButton;
static ImageView imageCapturedView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_image);




    captureImageButton = (Button) findViewById(R.id.button);

    OnClickListener captureImageListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent captureImageIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            if(captureImageIntent.resolveActivity(getPackageManager())!=null)
            {
                File photoFile=null;

                try{
                    photoFile = createImageFile();

                }catch(IOException e){};

                if(photoFile != null)
                {

                    captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    startActivityForResult(captureImageIntent, REQUEST_IMAGE_CAPTURE);
                }

            }

        }
    };

    captureImageButton.setOnClickListener(captureImageListener);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    imageCapturedView = (ImageView)findViewById(R.id.ImageView);

    if(requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK)
    {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap= (Bitmap) extras.get("data");

        imageCapturedView.setImageBitmap(imageBitmap);
        galleryAddPic();
        setPic();
    }
}

private File createImageFile() throws IOException
{

    String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());
    String ImageFile = "JPEG_" + TimeStamp + "_";
    File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(ImageFile, ".jpg", StorageDir); 

    mCurrentPhotoPath = image.getAbsolutePath();

    return image;

}

private void galleryAddPic()
{
    Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f= new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScan.setData(contentUri);
    this.sendBroadcast(mediaScan);

}


private void setPic() throws ArithmeticException{


    int scaleFactor;
    Bitmap bitmap;
    // Get the dimensions of the View
    int targetW = imageCapturedView.getWidth();
    int targetH = imageCapturedView.getHeight();


    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    //bmOptions.inSampleSize = 4;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
        scaleFactor= Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;



    bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    Toast.makeText(getApplicationContext(), mCurrentPhotoPath, Toast.LENGTH_LONG).show();
    imageCapturedView.setImageBitmap(bitmap);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.take_image, menu);
    return true;
}

}

Here the problem lies with the setPic function according to me but don't know what it is as i have just followed the tutorial given on developers.android

I am able to capture the image and store it in the sd card but not able to display it in the image view. Something going wrong during scaling the bitmap according to the image view properties.

This is the error that i am getting

01-20 14:32:34.736: E/AndroidRuntime(2513): FATAL EXCEPTION: main
01-20 14:32:34.736: E/AndroidRuntime(2513): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/JPEG_2014012020_143223_-1472164486.jpg typ=image/jpeg (has extras) }} to activity {yogesh.atArxxus.ocr_trial_application/yogesh.atArxxus.ocr_trial_application.Take_image}: java.lang.ArithmeticException: divide by zero
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3161)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3204)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.access$1100(ActivityThread.java:137)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.os.Looper.loop(Looper.java:213)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.main(ActivityThread.java:4793)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at dalvik.system.NativeStart.main(Native Method)
01-20 14:32:34.736: E/AndroidRuntime(2513): Caused by: java.lang.ArithmeticException: divide by zero
01-20 14:32:34.736: E/AndroidRuntime(2513):     at yogesh.atArxxus.ocr_trial_application.Take_image.setPic(Take_image.java:136)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at yogesh.atArxxus.ocr_trial_application.Take_image.onActivityResult(Take_image.java:87)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.Activity.dispatchActivityResult(Activity.java:5192)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3157)

Thank you

Avijit
  • 3,834
  • 4
  • 33
  • 45
Yogesh D
  • 1,663
  • 2
  • 23
  • 38
  • If I remove the scaling code and simply try to display the image in image view then it is working. Dont know whats wrong some how i am getting targetW targetH this values zero somewhere. – Yogesh D Jan 20 '14 at 09:08
  • I had also similar issues. so please check the below link, it may be useful for u. http://stackoverflow.com/questions/21136311/picking-a-photo-from-gallery-and-show-in-a-image-view – Biplab Jan 20 '14 at 09:25
  • @Biplab So did you use that image-chooser library...? – Yogesh D Jan 20 '14 at 09:56
  • Yes, i have used that and its working clean – Biplab Jan 20 '14 at 13:53
  • @Biplab Thanks the library is working fine, but actually my above code also accomplishes the thing... the only problem was with displaying the full image in the image view by scaling down its bitmap...in the library you mentioned it also provides the preview of the thumbnail and not the image. Is that right..? – Yogesh D Jan 22 '14 at 04:25
  • @y.dicit actually it depends on ur requirement if u want u can show image directly also.But when go to gallery through Ur app, the phone shows some of the deleted images which is in cache memory. I have an app i will provide u in some time the code but i have not tested it properly... – Biplab Jan 22 '14 at 05:11

2 Answers2

1

In the line "scaleFactor= Math.min(photoW/targetW, photoH/targetH);", either targetW or targetH or both are zero. Just include a simple validation like this, right before that line.

if(targetH == 0) targetH = 1;
if(targetW == 0) targetW = 1;

Your next step should be in debugging why one of those contain zero.

Srikanth
  • 2,014
  • 19
  • 22
  • Yes that is actually my problem... I debugged the program and knew that one of the value is becoming zero and so formula used for scaling gets denominator as 0 and app fails.... But not sure why the values are becoming 0..? No reason actually..? I am just getting the heaight and width of my imageView.. – Yogesh D Jan 22 '14 at 04:28
  • In the xml, is the ImageView (R.id.ImageView) having fixed dimensions for layout-width and layout-height? – Srikanth Jan 22 '14 at 04:36
  • 1
    Got it. Refer these answers please - http://stackoverflow.com/questions/4680499/android-get-height-width-of-imageview and http://stackoverflow.com/questions/10411975/how-to-get-the-width-and-height-of-an-image-view-in-android . Actually, a better way would be to store the dimensions in a dimensions-values xml and use that dimension to set your imageview's dimensions in xml layout file and also use the same dimensions (from values xml) to scale your image in setPic() method – Srikanth Jan 22 '14 at 04:48
  • @Srikanh ys got it....the mistake was in XML i was keeping width and height for image view as fillparent and wrapcontent so i was getting 0 values for both...if i keep fixed dp values for height and width then it works.....would even try with getMeasuredHeight and getMeasuredWIdth .....Thank You – Yogesh D Jan 23 '14 at 03:46
0

Try this but its not tested properly

public class MainActivity extends Activity {

    // Activity request codes
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    public static int RESULT_LOAD_IMAGE = 1; 

    // directory name to store captured images and videos
    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

    private Uri fileUri; // file url to store image/video

    private ImageView imgPreview;
    private VideoView videoPreview;
    private Button btnCapturePicture, btnRecordVideo;
    private ImageButton captureImage,browsImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgPreview = (ImageView) findViewById(R.id.imgPreview);
        videoPreview = (VideoView) findViewById(R.id.videoPreview);
        btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
        btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);
        captureImage = (ImageButton) findViewById(R.id.captureImage);
        browsImage = (ImageButton) findViewById(R.id.browsImage);


        /*
         * Capture image button click event
         */
        captureImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // capture picture
                captureImage();
            }
        });


        browsImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // capture pictur
                Intent i = new Intent(Intent.ACTION_PICK,  
                android.provider.MediaStore  
                              .Images.Media.EXTERNAL_CONTENT_URI);  
                startActivityForResult(i, RESULT_LOAD_IMAGE); 
                }
        });
        /*
         * Record video button click event
         */
        btnRecordVideo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // record video
                recordVideo();
            }
        });

        // Checking camera availability
        if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }
    }

    /**
     * Checking device has camera hardware or not
     * */
    private boolean isDeviceSupportCamera() {
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    /*
     * Capturing Camera Image will lauch camera app requrest image capture
     */
    private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

    /*
     * Here we store the file url as it will be null after returning from camera
     * app
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on scren orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

    /*
     * Recording video
     */
    private void recordVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

        // set video quality
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
                                                            // name

        // start the video capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    }

    /**
     * Receiving activity result method will be called after closing the camera
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image

        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // successfully captured the image
                // display it in image view
                previewCapturedImage();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // video successfully recorded
                // preview the recorded video
                previewVideo();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled recording
                Toast.makeText(getApplicationContext(),
                        "User cancelled video recording", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to record video
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                        .show();
            }
        }
        else if (requestCode == RESULT_LOAD_IMAGE) {

            super.onActivityResult(requestCode, resultCode, data);  

            if (resultCode == RESULT_OK && null != data) {  
                Uri selectedImage = data.getData();  
                String[] filePathColumn = { MediaStore.Images.Media.DATA };  

                Cursor cursor = getContentResolver().query(selectedImage,  
                        filePathColumn, null, null, null);  
                cursor.moveToFirst();  

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
                String picturePath = cursor.getString(columnIndex);  
                cursor.close();  
                Bitmap bm=BitmapFactory  
                        .decodeFile(picturePath);
                Bitmap pic=ShrinkBitmap(picturePath,100,100);

                imgPreview = (ImageView) findViewById(R.id.imgPreview);  
                imgPreview.setVisibility(View.VISIBLE);
                imgPreview.setImageBitmap(pic); 


            }  
        }
    }

    /*
     * Display image from a path to ImageView
     */
    private void previewCapturedImage() {
        try {
            // hide video preview
            videoPreview.setVisibility(View.GONE);

            imgPreview.setVisibility(View.VISIBLE);

            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 6;



            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);

            try {
            File file = new File (fileUri.getPath());
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
            imgPreview.setImageBitmap(bitmap);
             out.flush();
               out.close();
            } catch (Exception e) {
                   e.printStackTrace();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /*
     * Previewing recorded video
     */
    private void previewVideo() {
        try {
            // hide image preview
            imgPreview.setVisibility(View.GONE);

            videoPreview.setVisibility(View.VISIBLE);
            videoPreview.setVideoPath(fileUri.getPath());
            // start playing
            videoPreview.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ------------ Helper Methods ---------------------- 
     * */

    /*
     * Creating file uri to store image/video
     */
    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /*
     * returning image / video
     */
    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    Bitmap ShrinkBitmap(String file, int width, int height){

         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

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

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
         return bitmap;
        }

}

**XML FILE

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical" >

    <!-- Capture picture button -->

    <ImageButton
        android:id="@+id/captureImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/camera" />

    <ImageButton
        android:id="@+id/browsImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brows" />

    <Button
        android:id="@+id/btnCapturePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take a Picture"
        android:visibility="gone"
        android:layout_marginBottom="10dp"/>

    <!-- Record video button -->
    <Button
        android:id="@+id/btnRecordVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="Record a Video" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Preview" 
        android:padding="10dp"
        android:textSize="15dp"/>

    <!-- To display picture taken -->
    <ImageView
        android:id="@+id/imgPreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <!-- To preview video recorded -->
    <VideoView
        android:id="@+id/videoPreview"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:visibility="gone" />
</LinearLayout>

Biplab
  • 564
  • 1
  • 5
  • 19