3

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.

JJD
  • 50,076
  • 60
  • 203
  • 339
Manish Karena
  • 724
  • 6
  • 29

1 Answers1

1

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();
    }
});
JJD
  • 50,076
  • 60
  • 203
  • 339
mes
  • 3,581
  • 29
  • 28
  • I don't understand what you trying to say. – Manish Karena Feb 02 '15 at 07:32
  • Your code is incomplete here, I can't correct it and send back, but what I want to say, is that Android system uses two pass process for measuring views dimensions, and for example if You call getWidth() method in the onCreate method, it will return 0, because measurement process isn't done yet and view don't have a size – mes Feb 02 '15 at 07:38
  • Try call frame.getWidth() method in this method, and you'l see that it 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) { } }); – mes Feb 02 '15 at 07:44
  • Can you post error log? It seems that this is a different issue – mes Feb 02 '15 at 08:43
  • Using my code that i post in qusetion when trying to get image from camera in device that run on android 4.4.2 but when i try in device that run on android 4.1.2 then it work perfect. – Manish Karena Feb 02 '15 at 08:47
  • 1
    It is now clear what's the problem, I also had that problem after upgrading to KitKat, it's related to permission changes, try this link, this is solved my issue, if this also wont help, I try to send an working example from my project http://developer.android.com/training/camera/photobasics.html – mes Feb 02 '15 at 08:55
  • I already have put **** in android manifest. It give same error – Manish Karena Feb 02 '15 at 09:36
  • The log says that its related to width and height, did you try to set hardcoded width and height? The values that worked on Android 4.1.2 – mes Feb 02 '15 at 09:43