huxaiphaer Idris huxaiphaer Idris - 4 months ago 30
Java Question

Sending an Image from a RecyclerView to the nextActivity into an ImageView using Picasso

This is my Adapter Class, so in the

onBindViewHolder
method.

I am looking for a way to send the image loaded from
Picasso
to the
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);
}
}


This is my nextActivity, and how I can handle it here

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);
}
}

Answer

There is couple ways to do it , but in my project i am using this way :

First of all get Bitmap from ImageView, convert it to base64 String and pass String via Intent.

You can achieve it like that:

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

Your decode and encode methods :

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);
}

In SecondActivity :

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

@Edit regarding to your comment :

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)); 
    }
});
Comments