Listener on Image View

Put your problem here if it does not fit any of the other categories.

Listener on Image View

Postby Pushpa.Prakruthi » Sat Jan 12, 2008 8:53 am

Hi,
I wrote Program to display Image using Gallery and as i focus image through arrow keys I need to display there path in EditText Box (when Image is focusable).I done it, but it is not working on my expectation.Please let me now what i miss in my code.

And also,Is there any way to display multiple picture and when that images is clicked that path or image is loaded to particular file.
I attached my code
Attachments
Demo_Image.zip
(62.4 KiB) Downloaded 370 times
Pushpa.Prakruthi
Freshman
Freshman
 
Posts: 3
Joined: Wed Jan 09, 2008 9:38 am
Location: India,Bangalore

Top

Postby plusminus » Sat Jan 12, 2008 11:15 am

Hello Pushpa.Prakruthi,

your problem is the following line:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. str=i.getResources().getString(mImageIds[position]).toString();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Because getResources().getString(int) is awaiting the ID of a String-Resource :!: it is not there to determine the FilePath of the ID passed.

The second thing is, that the images you place under "/res/drawable/" get compiled into you Application, and therefore there is no actual FilePath you can determine :(

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Listner on Image view

Postby Pushpa.Prakruthi » Sat Jan 12, 2008 11:26 am

Hi,

Thanks for your guidence,then how could i resolve my problem.
That is onclick on the focusable image i need to display the Image name and load that image to file.
It is similar as Browse for the image and attach(load) to the file.
Pushpa.Prakruthi
Freshman
Freshman
 
Posts: 3
Joined: Wed Jan 09, 2008 9:38 am
Location: India,Bangalore

Re: Listner on Image view

Postby plusminus » Sat Jan 12, 2008 5:19 pm

Pushpa.Prakruthi wrote: i need to display the Image name and load that image to file.

You meant like:

Displaying: "myImage.png" and >>save<< it to an 'external' file, like on SDCard :?:

I was not aware of a function returning the name (what is the filename without the file-ending) of a known resource (knowing only its ID), so I coded one myself:
Example-Call:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Example-Call:
  2. String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);
  3. // resName would be 'icon'.
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

The Source:
Using the Java Reflection API this was not hard to do.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /**
  2.          * Determines the Name of a Resource,
  3.          * by passing the <code>R.xyz.class</code> and
  4.          * the <code>resourceID</code> of the class to it.
  5.          * @param aClass : like <code>R.drawable.class</code>
  6.          * @param resourceID : like <code>R.drawable.icon</code>
  7.          * @throws IllegalArgumentException if field is not found.
  8.          * @throws NullPointerException if <code>aClass</code>-Parameter is null.
  9.          * <br><br>
  10.          * <b>Example-Call:</b><br>
  11.          * <code>String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);</code><br>
  12.          * Then <code>resName</code> would be '<b>icon</b>'.*/
  13.         private String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
  14.                                                 throws IllegalArgumentException{
  15.                 /* Get all Fields from the class passed. */
  16.                 Field[] drawableFields = aClass.getFields();
  17.                
  18.                 /* Loop through all Fields. */
  19.                 for(Field f : drawableFields){
  20.                         try {
  21.                                 /* All fields within the subclasses of R
  22.                                  * are Integers, so we need no type-check here. */
  23.                                
  24.                                 /* Compare to the resourceID we are searching. */
  25.                                 if (resourceID == f.getInt(null))
  26.                                         return f.getName(); // Return the name.
  27.                         } catch (Exception e) {
  28.                                 e.printStackTrace();
  29.                         }
  30.                 }
  31.                 /* Throw Exception if nothing was found*/
  32.                 throw new IllegalArgumentException();
  33.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So all you requested would be like the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 try {
  2.                         String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);
  3.                        
  4.                         Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
  5.                         bm.compress(Bitmap.CompressFormat.PNG, 100, openFileOutput(resName + ".png", MODE_WORLD_READABLE));
  6.                 } catch (FileNotFoundException e) {
  7.                         e.printStackTrace();
  8.                 }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Let us know if it helped you :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 33 guests