cancel
Showing results for 
Search instead for 
Did you mean: 

Fetching Images Via JAVA API

Former Member
0 Kudos

Hi,

I am trying to fetch images via Java Api.

Please look at the code below:


A2iValueArray imgs1 = null;
for(int y=0; y<rs1.GetRecordCount(); y++){
	imgs1 = rs1.GetValueAt(y,"Product_Image").GetValueArray();
}

It is throwing me a Class cast exception.

I also tried

imgs1 = (A2iValueArray) rs1.GetValueAt(y,"Product_Image").GetValueArray();

but to no avail...

I am using MDM Sp04..

Please guide me...

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Thank you for your reply..

I am using the code from that weblog only...

No idea why is it failing....

Former Member
0 Kudos

Hi,

The first thing you need to check is whether the field you are trying to retrieve is single-value or multi-value.

If it is single-value, then you should get the ID by:

..GetValueAt(i, FIELD_NAME).GetIntValue()

Walter

Former Member
0 Kudos

Walter,

The field is Multi-valued...

I am having the following code with me...


  A2iValueArray imgs1 = new A2iValueArray();
	for(int y=0; y<rs1.GetRecordCount(); y++){
		if(!rs1.GetValueAt(y, "Product_Color").IsNull()){
		imgs1 = rs1.GetValueAt(y,"Product_Color").GetValueArray();
		int id = imgs1.GetValueAt(0).GetIntValue();
		//int id = rs1.GetValueAt(0, "Product_Color").GetIntValue();
		String imagePath = catalogCache.GetImagePath("Products", id);//"Thumbnail" ,
		wdComponentAPI.getMessageManager().reportWarning("Your image is at: " + CACHE_DIRECTORY + "\" + imagePath);
		break;
		}

I get a ClasscastException at imgs1 = rs1.GetValueAt(y,"Product_Color").GetValueArray();

Really unable to understand why is Casting reqd in first place?

Any pointers will be appreciated...

Former Member
0 Kudos

I would do it this way:

Value val = rs1.GetValueAt(y,"Product_Color");

if(val != null) {

imgs1 = val..GetValueArray();

}

Reason being that if there are no images, val is going to be null and with your approach, it's going to throw NullPointer or ClassCast Exception.

HTH

--

Venkat

Former Member
0 Kudos

Venkat,

I tried it with same output.....

I guess its same thing... if you look at your "var" variable and substitute it with the entire value... your code is completely similar to mine...

Moreover, if val is null or not initialised, it will throw a Null Pointer Exception...

While I am not too sure why is it throwing Classcast Exception...

Former Member
0 Kudos

This Code Worked for me:


		try {	
		A2iResultSet rs1 = catalogData.GetResultSet(search, rsd, null, true, 0);
		 A2iValueArray imgs1 = new A2iValueArray();
			for(int y=0; y<rs1.GetRecordCount(); y++){
				if(!rs1.GetValueAt(y, "Images").IsNull()){
				imgs1 = rs1.GetValueAt(y,"Images").GetValueArray();
				int id = imgs1.GetValueAt(0).GetIntValue();
				//int id = rs1.GetValueAt(0, "Product_Color").GetIntValue();
				String imagePath = catalogCache.GetImagePath("Products", id);//"Thumbnail" ,
				//wdComponentAPI.getMessageManager().reportWarning("Your image is at: " + CACHE_DIRECTORY + "\" + imagePath);
				System.out.println("imagePath="+imagePath);
				break;
				}
			}
	
			catalogData.Logout();
		} catch (StringException e) {
			e.printStackTrace();
		}

Posting your program may help troubleshoot.

--

Venkat

Former Member
0 Kudos

I assume Images is the field in the table.. right?

But for me same code results in an error...

What is the way to store images in MDM... i guess that might be stored wrongly..

As the type of data returned should match with A2iValueArray but it does not...

Former Member
0 Kudos

Any more inputs guys?

Am stuck here for last 2 days...

Thanks for your contribution so far....

- Dev

Former Member
0 Kudos

Are you sure you have an image in the record? Convert your code to two steps, first checking for null, as in the following example:


Value imageVal = rs1.GetValueAt(y,"Images");
if( imageVal != null && !imageVal.IsNull())
{
    A2iValueArray arr = imageVal.GetValueArray();
    // ... etc.
}

Walter

Former Member
0 Kudos

Hi Walter,

There have been certain issues with MDM server so havent been able to test the application with your code.

But will test soon... and will let you know the results...

Regards,

Dev.

Former Member
0 Kudos

I changed APIs and used CatalogData's getImage method to fetch images and it works fine...

Thank you guys for your help!

sridhar_k2
Active Contributor
0 Kudos

Hi,

Go thru this link.

/people/walter.kahn/blog/2005/12/29/mdm-55-api-tips-and-tricks--image-retrieval

Regards,

Sridhar