How to get image height and width before rendering it onto screen.

Hello friends,
   
        I am working on a android project. In this I need to get image height and width before it display on screen.
So that if any one know how to get the image original height and width before displaying them, then please help me.
        Here I attach some code , in which the image height and width is not get initially why this is happen and also until I am not display the second child by view flipper there size also not get. So please tell me where I am wrong or which method will give me the image height and width at that moment.

Thank you.
//The java code 

package Volga.Myproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class Myproject extends Activity {
	ViewFlipper viewFlipper;
	Button child0,child1;
	ImageView imgChild0,imgChild1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        viewFlipper=(ViewFlipper)findViewById(R.id.MainViewFlipper);
        child0=(Button)findViewById(R.id.btnChild0);
        child1=(Button)findViewById(R.id.btnChild1);
        imgChild0=(ImageView)findViewById(R.id.imgChild0);
        imgChild1=(ImageView)findViewById(R.id.imgChild1);
        
        System.out.println("Child 0 image Height"+imgChild0.getHeight()+" Width"+imgChild0.getWidth());
        System.out.println("Child 1 image Height"+imgChild1.getHeight()+" Width"+imgChild1.getWidth());
        System.out.println("************At FIRST THE H:W IS ZERO**********************");
        
        child0.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				viewFlipper.setDisplayedChild(0);
				System.out.println("Child 0 image Height"+imgChild0.getHeight()+" Width"+imgChild0.getWidth());
				if(imgChild0.getHeight()==0)
					System.out.println("************At FIRST THE H:W IS ZERO**********************");
				else
					System.out.println("***********Image Is previously loaded at this movment so that not zero************");
			}
		});
        
		child1.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						viewFlipper.setDisplayedChild(1);
						System.out.println("Child 1 image Height"+imgChild1.getHeight()+" Width"+imgChild1.getWidth());
						if(imgChild1.getHeight()==0)
							System.out.println("************At FIRST THE H:W IS ZERO**********************");
						else
							System.out.println("***********Image Is previously loaded at this movment so that not zero************");
					}
				});
    }
}




//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >  
			<Button  
			    android:layout_width="wrap_content" 
			    android:layout_height="wrap_content" 
			    android:text="Child0"
			    android:id="@+id/btnChild0"
			    />
			 <Button  
			    android:layout_width="wrap_content" 
			    android:layout_height="wrap_content" 
			    android:text="Child1"
			    android:id="@+id/btnChild1"
			    />  
    
  </LinearLayout>  
   <ViewFlipper
						  android:orientation="vertical"
						  android:layout_width="fill_parent"
						  android:layout_height="fill_parent"
						  android:id="@+id/MainViewFlipper"
						  >
  
							    <LinearLayout android:orientation="vertical"
							  				android:layout_width="fill_parent"
							  				android:layout_height="fill_parent"
							  				android:id="@+id/child1">
											<ImageView  
											    android:layout_width="wrap_content" 
											    android:layout_height="wrap_content" 
											    android:src="@drawable/android"
											    android:id="@+id/imgChild0"
											    />
								</LinearLayout>	 <!-- child 0 default child --> 
								
								<LinearLayout android:orientation="vertical"
							  				android:layout_width="fill_parent"
							  				android:layout_height="fill_parent"
							  				android:id="@+id/child2">
							  				<ImageView  
											    android:layout_width="wrap_content" 
											    android:layout_height="wrap_content" 
											     android:src="@drawable/android_p"
											    android:id="@+id/imgChild1"/>
								</LinearLayout> <!-- child 1 child --> 
	
				</ViewFlipper>	 
    
</LinearLayout>

Open in new window

jbajajAsked:
Who is Participating?
 
Dejan PažinHead of SW DevelopmentCommented:

You should first read the image via BitmapFactory to obtain the Bitmap object, on that object you can see the original width and height of the image.

Use decodeStream for that:

BitmapFactory.decodeStream(new FileInputStream(file), null, null);

Getting measures from a View is not relevant, specially before it is drawn on the screen, because it has no width and height at that time and later on when it is displayed, the measures are those of the object on the screen not of the original image.
0
 
jbajajAuthor Commented:
Hi dejanpazin,

  Thank you for reply, and its work fine for me  your simply great man. Thanks ones again.

  I just want to ask one more question , that how to see the  memory allocation or heap memory of our application. Because In my application I display big size(4 to 5 Mb) image by decoding them. but when I change this image by 6 to 7 times  then out of memory problem is occur. So I just want check whether my previous allocation is release or not. so please tell me how to check memory allocation in android.
Or if any tool available to see this.
(In logcat there is some heap but dont know how to handle.)
0
 
Dejan PažinHead of SW DevelopmentCommented:

Its a common problem with bitmaps, I had it too. I scaled the image down, thats how I avoided the problem. It is also what is suggested here:

http://stackoverflow.com/questions/2131947/android-memory-allocation

About memory allocation, here is a very detailed answer:

http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android

In one sentence: it is not easy to get the memory allocation.

You should also know that an application can not allocate as much memory as on desktop, limit is much lower:

http://stackoverflow.com/questions/3590443/what-is-the-maximum-memory-limits-per-application-for-android-2-2
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.