简体   繁体   中英

How do I pass a downloaded image from one activity to the next?

I would like to take a downloaded image the user clicked on from on activity to the next. I believe this should be done in my lambda onItemClickListenerClass.

My code for my custom adapter class is here:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Movie movie = getItem(position);
      if  ( movie == null) return null;


        ImageView imageView;
        /**
         * if convertView is empty, set it equal to ImageView
         */
        if (convertView == null) {
            imageView = new ImageView(context);


            imageView.setLayoutParams(new GridView.LayoutParams(width, height));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            imageView = (ImageView) convertView;
        }

        Uri coverUrl = movie.getMovieCover();
        Picasso.with(context)
                .load(coverUrl)
                .into(imageView);

        //    Log.e(LOG_TAG, "URL " + url);


        return imageView;
    }

and this is code from my Object constructor class

public Uri getMovieCover(){
      final String BASE = "http://image.tmdb.org/t/p/w185";

    Uri builtUri = Uri.parse(BASE).buildUpon()
            .appendEncodedPath(poster_path)
            .build();

    return builtUri;
}

I have bundled all my string data into an extra and passed it through an intent, and I was thinking of doing the same thing in this context by converting the image into an extra and pass it through the intent, but I currently do not know how to implement that.

Can anyone help me with this problem? I am currently doing the Udacity Android Programmer final project so I still am fairly new to android programming.

Pass the Url as String extra to 2nd Activity.

In 1st Activity/Custom Adapter :

String url = "http://image-url";
Intent intent = new Intent (getActivity().this, 2ndActivity.class);
intent.putExtra("image-url", url);
getActivity().startIntent(intent);

In 2nd Activity :

String url;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent i = getIntents();
    url = i.getExtra("image-url");

    Uri coverUrl = new Uri(url);

    if (url.Length() > 0) {
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(width, height));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        Picasso.with(context)
                .load(coverUrl)
                .into(imageView);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM