Rain Man
Rain Man

Reputation: 1273

Access image path for NetworkImageView

I am trying to get the image path of a NetworkImageView form a ListView but it returns null. In my MainActivity.java I use the following:

   listView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      // get the title
      String _movieTitle = ((TextView) view.findViewById(R.id.title)).getText().toString();
      // send the title
      myIntent.putExtra("title", _movieTitle) ;

      // get the image path
      NetworkImageView v = (NetworkImageView) view.findViewById(R.id.thumbnail);
      String imgUrl = (String) v.getTag();
      ....
      // just for testing (works fine)
      System.out.print("title is " + _movieTitle);
      // image path is (returns null)
      System.out.print("Image path is " + imgUrl);
    }
}

content of adapter.java is as follow:

public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        ....
        // getting movie data for the row
        Movie m = movieItems.get(position);

       // thumbnail image
       thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

       // title
       title.setText(m.getTitle());

content of list.xml :

 <!-- Thumbnail Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/thumbnail"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp" />

    <!-- Movie Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

not sure why the image path returned in MainActivity.java is null. Can someone please have a look at this and see what I am doing wrong?

Upvotes: 0

Views: 471

Answers (2)

Toris
Toris

Reputation: 2386

New answer.

Set view.tag when you call setImageUrl()

In adapter.java

String imgUrl = m.getThumbnailUrl();
thumbNail.setImageUrl(imgUrl, imageLoader);
thumbNail.setTag(imgUrl);

I've checked current (master tree) version NetworkImageView and it does not set tag (always null).


OLD answer.

MainActivity.java

String imgUrl = (String) v.getTag();

Chage to

ImageContainer c = (ImageContainer) v.getTag();
String imgUrl = (null == c) ? "" : c.getRequestUrl();

from NetworkImageView source

NetworkImageView.setImageUrl() or onLayout() calls
NetworkImageView.loadImageIfNecessary() {
    ...
    ImageContainer newContainer = mImageLoader.get(mUrl, ...
    setTag(newContainer);
    ...
}

from NetworkImageView source

public class NetworkImageView extends ImageView {
/** The URL of the network image to load */
private String mUrl;
...

ImageContainer oldContainer = (ImageContainer) getTag();
// if there was an old request in this view, check if it needs to be canceled.
if (oldContainer != null && oldContainer.getRequestUrl() != null) {
    if (oldContainer.getRequestUrl().equals(mUrl)) {
    // if the request is from the same URL, return.
    return;
...
}

This shows "oldContainer.getRequestUrl()" can be compaired with "mUrl" and it means getTag().getRequestUrl() returns url.

Upvotes: 0

SilentKnight
SilentKnight

Reputation: 14021

Why do too much unnecessary? Just do String imageUrl = NetworkImageView.getImageURL();

Upvotes: 1

Related Questions