Search code examples
androidandroid-framelayoutandroid-camera-intent

layout getHeight() and getWidth() return 0 when getting image from camera


I m creating a photo-frame app when i m getting image from gallery than it's working fine but when i try to get image from camera then my framelayout.getwidth() and framelayout.getHeight() method returns 0.

My code given below:

btnGetImage.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
            imagePickerDialog();
    }
});

public void imagePickerDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Pictures Option");
    myAlertDialog.setIcon(R.drawable.getimage_icon);
    myAlertDialog.setMessage("Select Picture From...");
    myAlertDialog.setPositiveButton("Gallery",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent fileIntent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(fileIntent,
                        Constant.FILE_REQUEST);
            }
        });
    myAlertDialog.setNegativeButton("Camera",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent,
                        Constant.CAMERA_REQUEST);
            }
        });
    myAlertDialog.show();
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Constant.CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap camera_image = (Bitmap) data.getExtras().get("data");
        addViewInFrame(camera_image);               
    } else if (requestCode == Constant.FILE_REQUEST
            && resultCode == RESULT_OK) {

        Uri selectedImage = data.getData();
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor c = getContentResolver().query(selectedImage, filePath,
                null, null, null);
        c.moveToFirst();
        int columnIndex = c.getColumnIndex(filePath[0]);
        String picturePath = c.getString(columnIndex);
        c.close();
        addViewInFrame(getScaledBitmap(picturePath, 300, 300));
    }
}

public void addViewInFrame(Bitmap viewBit) {
    if (android.os.Build.VERSION.SDK_INT <= 10) {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenWidth = metrics.widthPixels;
        int screenHeight = metrics.heightPixels;

        imageBitmap = Bitmap.createScaledBitmap(viewBit, screenWidth,
            screenHeight / 2, true);
        image1 = new TouchImageView(getApplicationContext());
        image1.setScaleType(ScaleType.MATRIX);
        image1.setImageBitmap(imageBitmap);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            screenWidth, screenHeight / 2);
        params.leftMargin = 0;
        params.topMargin = 0;
        frame.addView(image1, params);
    else {
        imageBitmap = Bitmap.createScaledBitmap(viewBit,
            frame.getWidth(), frame.getHeight() / 2, true);
        image1 = new TouchImageView(getApplicationContext());
        image1.setScaleType(ScaleType.MATRIX);
        image1.setImageBitmap(imageBitmap);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                frame.getWidth(), frame.getHeight() / 2);
        params.leftMargin = 0;
        params.topMargin = 0;
        frame.addView(image1, params);
}

Here frame is my framelayout.

Error Log is:

Process: com.example.imageoverotherimage, PID: 3065
java.lang.RuntimeException: Unable to resume activity 
{com.example.imageoverotherimage/com.example.imageoverotherimage.MainActivity}: 
java.lang.RuntimeException: Failure delivering result 
ResultInfo{who=null, request=1888, result=-1, data=Intent { 
act=inline-data dat=content://media/external/images/media/6658 (has extras) }} 
to activity {com.example.imageoverotherimage/com.example.imageoverotherimage.MainActivity}: 
java.lang.IllegalArgumentException: width and height must be > 0

    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2996)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3025)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3974)
    at android.app.ActivityThread.access$1000(ActivityThread.java:166)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.RuntimeException: Failure delivering result 
ResultInfo{who=null, request=1888, result=-1, data=Intent { 
act=inline-data dat=content://media/external/images/media/6658 (has extras) }} 
to activity {com.example.imageoverotherimage/com.example.imageoverotherimage.MainActivity}: 
java.lang.IllegalArgumentException: width and height must be > 0

    at android.app.ActivityThread.deliverResults(ActivityThread.java:3601)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2983)
    ... 13 more

Caused by: java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:922)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:833)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:709)
    at com.example.imageoverotherimage.MainActivity.addViewInFrame(MainActivity.java:232)
    at com.example.imageoverotherimage.MainActivity.onActivityResult(MainActivity.java:187)
    at android.app.Activity.dispatchActivityResult(Activity.java:5514)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3597)
    ... 14 more

Thanks.


Solution

  • You should call getWidth() and getHeight() methods after or inside onLayout() event. Here is more detailed description of that process How Android Draws Views.

    Try to call frame.getWidth() in this method, and you'll see that it's result differs from 0.

    frame.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, 
                                   int right, int bottom, 
                                   int oldLeft, int oldTop, 
                                   int oldRight, int oldBottom) {
            int w = frame.getWidth();
        }
    });