Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

fetch image using JCR or Query Builder

Avatar

Level 2

Hi,

I have implemented simple program for fetching image using JCR. 

ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
Resource res = resourceResolver.getResource("/content/dam/geometrixx-media/articles/bored.jpeg");            
Asset asset = res.adaptTo(Asset.class);

I am able to display image on JSP as asset.getPath() gives me path of image. But all this works fine only if I mention complete path of image. If I provide only name of the image (bored.jpeg) then it does not work. 

As a solution to this problem I used query builder API. Followed link http://helpx.adobe.com/experience-manager/using/aem-dam-image-components.html    

But code snippet provided in this link retrieves all images matching to search criteria. Because I wanted to search for particular image I updated query parameters as follows,

Map<String, String> map = new HashMap<String,String>();
map.put("type", "dam.Asset");
map.put("property", "cq:name");
map.put("property.value", "ai.jpeg");
qb=resource.getResourceResolver().adaptTo(QueryBuilder.class);
Query query = qb.createQuery(PredicateGroup.create(map), session);

SearchResult sr= query.getResult();
String assetPath=null; 

 // iterating over the results
Iterator<Resource> resources = sr.getResources();
  while(resources.hasNext() ) {
      Resource res = resources.next();
      Asset asset = res.adaptTo(Asset.class);     
      String Path = asset.getPath();

which led to an exception because Asset object is null. Resource object gives me path as follows "/content/dam/geometrixx-media/articles/ai.jpeg/jcr:content"  I am not getting why path is appended with /jcr:content when I searched for particular image. This does not happen when I search for images matching jpeg type or some other general search as given in following link  http://helpx.adobe.com/experience-manager/using/aem-dam-image-components.html   

Kindly help me in resolving this issue.

Thanks,

Manju

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi,

Some of the properties that you are searching for is defined in the jcr:content part of the Asset so that is the node you will get.

If you instead actuall states that it is the jcr:content:property you want, then you will get the Asset node. Something like this.

map.put("type", "dam.Asset");
map.put("property", "jcr:content/metadata/dc:format");
map.put("property.value", "image/jpeg");


However... you want to find the node with the name ai.jpg. Why not ask for it??

type=dam:Asset
path=/content/images/flags
nodename=gh.png

This returns the node with the name gh.png under the path /content/images/flags. Since you don't really know how the node name is stored by CQ, you should use the JCR defined search parameter nodename instead.

/Ove

View solution in original post

2 Replies

Avatar

Correct answer by
Level 6

Hi,

Some of the properties that you are searching for is defined in the jcr:content part of the Asset so that is the node you will get.

If you instead actuall states that it is the jcr:content:property you want, then you will get the Asset node. Something like this.

map.put("type", "dam.Asset");
map.put("property", "jcr:content/metadata/dc:format");
map.put("property.value", "image/jpeg");


However... you want to find the node with the name ai.jpg. Why not ask for it??

type=dam:Asset
path=/content/images/flags
nodename=gh.png

This returns the node with the name gh.png under the path /content/images/flags. Since you don't really know how the node name is stored by CQ, you should use the JCR defined search parameter nodename instead.

/Ove

Avatar

Level 2

Hi Ove,

Whether nodename is name of property? I tried executing my program with below code but query does not fetch any data. Also I didnt find any property with name 'nodename' in properties tab of CRXDE

map.put("type", "dam.Asset");
map.put( "property" , "jcr:content/metadata/dc:format" );
map.put( "property.value" , "image/jpeg" );
map.put("property", "nodename");
map.put("property.value", "ai.jpeg");

I also tried this line

map.put("nodename", "ai.jpeg");  This line throws an exception.

You have mentioned path of the image/node in code given above, but I don't want to specify any path in query parameters. 

I want to search for an image only passing its name or any other unique property of an image but not path, is there any way to achieve this? 

Thanks,

Manju