使用Picasso将图像从RecyclerView发送到nextActivity到ImageView

时间:2016-08-02 18:35:18

标签: java android android-intent bitmap picasso

这是我的适配器类,所以在onBindViewHolder方法中 我正在寻找一种方法将从Picasso加载的图片发送到NextActivity

package adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import oerrride.we.huzykamz.ugentapp.CinemaViewHolders;
import oerrride.we.huzykamz.ugentapp.ItemObjects;
import oerrride.we.huzykamz.ugentapp.MovieScrollingActivity;
import oerrride.we.huzykamz.ugentapp.NtindaCinemaViewHolders;
import oerrride.we.huzykamz.ugentapp.R;

/**
  * Created by HUZY_KAMZ on 7/22/2016.
 */
public class NtindaCinemaAdapter  extends     RecyclerView.Adapter<NtindaCinemaViewHolders> {
  private   String url="http://10.0.2.2/UgandaEntertainment/Ntinda/NtindaPics/";
private List<ItemObjects> itemList = null;
private Context context;
public View root = null ;


private ArrayList<ItemObjects> arraylist;
public NtindaCinemaAdapter(Context context, List<ItemObjects> itemList) {
    this.itemList = itemList;
    this.context = context;
}

@Override
public NtindaCinemaViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ntinda_cinema_list, parent,false);
    NtindaCinemaViewHolders rcv = new NtindaCinemaViewHolders(layoutView,context);
    return rcv;
}

@Override
public void onBindViewHolder(NtindaCinemaViewHolders holder, final int position) {
  final   ItemObjects feedItem = itemList.get(position);
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(NtindaCinemaAdapter.this.context, MovieScrollingActivity.class);
                intent.putExtra(NtindaCinemaViewHolders.KEY_SYNOPSIS, feedItem.getSynopsis());
                intent.putExtra(NtindaCinemaViewHolders.KEY_MOVIE_IMAGE, feedItem.getPhoto()+url);

                context.startActivity(intent);
            }
        });



    //Download image using picasso library

    Picasso.with(context).load(url+feedItem.getPhoto())
            .error(R.drawable.placeholder).
            resize(500,500)
            .placeholder(R.drawable.placeholder)

            .into(holder.moviepic);

    holder.moviename.setText(itemList.get(position).getMovieName());

}

@Override
public int getItemCount() {
    return (null != itemList ? itemList.size() : 0);
}
 }

这是我的nextActivity,以及我如何处理它

package oerrride.we.huzykamz.ugentapp;

import android.content.Intent;
import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;

public class MovieScrollingActivity extends AppCompatActivity {

    TextView      synpsis_txt;
    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scrolling);

        getSupportActionBar().setDisplayShowHomeEnabled(false);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.hide();

// declarations
        Intent getImage = getIntent();
        String gettingImageUrl = getImage.getStringExtra(NtindaCinemaViewHolders.KEY_MOVIE_IMAGE);
        synpsis_txt = (TextView) findViewById(R.id.synopsis_txt);
        image = (ImageView) findViewById(R.id.image_movie_scroll);

        Bundle bundle = getIntent().getExtras();
        synpsis_txt.setText(bundle.getString(NtindaCinemaViewHolders.KEY_SYNOPSIS));
        Picasso.with(MovieScrollingActivity.this).load(gettingImageUrl).error(R.drawable.placeholder).
                resize(500,500)
                .placeholder(R.drawable.placeholder).into(image);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab2);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到,但在我的项目中我使用这种方式:

首先从Bitmap获取ImageView,将其转换为base64 String并通过String传递Intent

你可以这样做:

Bitmap attachedbitmap= ((BitmapDrawable) imageview.getDrawable()).getBitmap();
Intent nextIntent=new Intent(this,SecondActivity.class);
nextIntent.putExtra("image",encodeTobase64(attachedbitmap));

您的解码和编码方法:

public static String encodeTobase64(Bitmap image) {
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    // Log.e("LOOK", imageEncoded);
    return imageEncoded;
}

public static Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

在SecondActivity中:

Bundle extras=getIntent().getExtras();
String imageString=extras.getString("image");
Bitmap incomingBitmap=decodeBase64(imageString);

@Edit关于您的评论:

holder.root.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(NtindaCinemaAdapter.this.context, MovieScrollingActivity.class);
        Bitmap attachedbitmap= ((BitmapDrawable) holder.moviepic.getDrawable()).getBitmap();
        intent.putExtra("image",encodeTobase64(attachedbitmap)); 
    }
});