How to set the image of row RecyclerView Adapter - android

I need to set the image of my row RecyclerView how to add the image of employee I want to convert the String to Bitmap and display the image in an imageView
Activity 1
String name = currentobject.getString("name_related");
String email = currentobject.getString("work_email");
String phone = currentobject.getString("work_phone");
String Img = currentobject.getString("image_small");
byte[] imageAsBytes = Base64.decode(Img.getBytes(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
employee= new Employee(id,(String)deptArry.get(1),email,phone,decodedByte.toString(),name);
Employees.add(employee);
ClassAdapter how to add the image of employee
public void onBindViewHolder(EmployeeHolder holder, int position) {
Employee employee=EmployeeList.get(position);
holder.id.setText(employee.getId());
holder.name.setText(employee.getName());
holder.phone.setText(employee.getPhone());
holder.Image.set????(employee.getImage());
}
Employee object with getter and setter ...
private String id;
private String name;
private String phone;
private String Image;

Your ViewHolder should have an ImageView as part of it's layout. Right now you are trying to set the image on "Image", but "Image" is type String.
Once you have an ImageView as part of your layout then you can simply call:
imageView.setImageBitmap(decodedByte)

Related

Preview BitMap in ImageView from a Base64 string

I tried to store a BitMap in the database as Base64 string, but somehow I just cant preview it in the ImageView. Here's the code:
Store Data:
Map params = new HashMap();
params.put("faasid", faasid);
params.put("title", data_title);
params.put("image", Base64.encodeToString(DbBitmapUtility.getBytes(bitmap),Base64.DEFAULT));
ImageDB db = new ImageDB();
db.create(params);
Retrieve Data:
List<ImageItem> data = new ArrayList<ImageItem>();
Map params = new HashMap();
params.put("faasid", faasid);
ImageDB db = new ImageDB();
List<Map> list = db.getList(params);
for(Map m : list){
String title = m.get("title") != null ? m.get("title").toString() : "";
String image = m.get("image") != null ? m.get("image").toString() : "";
data.add(new ImageItem(faasid, title, image.getBytes()));
}
image_list.setAdapter(new ImageItemAdapter(activity,data));
LayoutParams layout = (LayoutParams) image_list.getLayoutParams();
layout.height = (320 * data.size());
image_list.setLayoutParams(layout);
ImageItem.java:
public class ImageItem {
private String faasid, title;
private byte[] image;
public ImageItem(String faasid, String title, byte[] image){
this.faasid = faasid;
this.title = title;
this.image = image;
}
public String getFaasId(){ return faasid; }
public String getTitle(){ return title; }
public Bitmap getImage(){ return DbBitmapUtility.getImage(image); }
}
DbBitmapUtility.java
public class DbBitmapUtility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 80, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
ImageItemAdapter.java (my custome ListView adapter)
public class ImageItemAdapter extends BaseAdapter{
LayoutInflater inflater = null;
Context ctx;
List<ImageItem> data;
public ImageItemAdapter(Activity activity, List<ImageItem> data){
ctx = activity;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int pos) {
return pos;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.image_menu, null);
ImageView image = (ImageView) rowView.findViewById(R.id.image_menu_view);
TextView title = (TextView) rowView.findViewById(R.id.image_menu_text);
ImageItem item = data.get(pos);
if(item != null){
image.setImageBitmap(item.getImage());
title.setText(item.getTitle());
}
return rowView;
}
public ImageItem getListItem(int pos){
return data.get(pos);
}
I print the result of this code: List list = db.getList(params);, and there was data in it, but the problem is: The image wont appear in the ImageView.
The problem is you encode your image into a Base64 String, but you never actually decode it.
For the decoding:
String encodedBytes = params.get("image");
byte[] decodedBytes = Base64.decode(encodedBytes, Base64.DEFAULT);
Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
The actual flow should be something like this:
Bitmap -> byte array -> Base64 encoded String -> To DB.
From DB -> Base64 encoded String -> decoded byte array -> decoded Bitmap.

sending a customized object to intent android

I want to pass an object of type Annonce to an Intent. As you can see, it's simple if the class attributes are primitives, However; in my case I have an image (Bitmap) and an attribute of type Client ( I have created a Client class).
My solution is to access the Client attributes (using getter and setter) and parsing it in the writeToParcel method one by one (it takes too much time), and for the image, I am sending it in the mainActivity using ByteArrayOutputStream. Can anyone help me do it all in Annonce class.
public class Annonce implements Parcelable {
String article, desc, temps, ville, categorie;
int prix;
Bitmap img;
Client c;
public Annonce(String article, String desc, String temps, String ville,
String categorie, int prix, Bitmap img, Client c) {
this.article = article;
this.desc = desc;
this.c = c;
this.prix = prix;
this.img = img;
this.temps = temps;
this.categorie = categorie;
this.ville = ville;
}
public static final Parcelable.Creator<Annonce> CREATOR = new Parcelable.Creator<Annonce>() {
public Annonce createFromParcel(Parcel source) {
return new Annonce(source);
}
public Annonce[] newArray(int size) {
return new Annonce[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(article);
parcel.writeString(desc);
parcel.writeString(temps);
parcel.writeString(ville);
parcel.writeString(categorie);
parcel.writeInt(prix);
}
public Annonce(Parcel source) {
article = source.readString();
desc = source.readString();
temps = source.readString();
ville = source.readString();
categorie = source.readString();
prix = source.readInt();
}
}
Having an attribut of type "bitmap" is not a good solution . Instead of that , we can use the path of the image to refer to the bitmap image .
Also, we can convert the Client into object in parcelable in order to send it through intent.

How to send listview item image from one activity to another using xml parser

I have made an app in which I am retrieving data from weburl using xmlparsing in a listview (with image and few text fields) and I wrote code for onItemClick and values displays to another activity, but I am not able to send image from one activity to another, but sending text data by using listview item.
Can someone guide me and write some code, with little bit of description also?
Here is some of my code:
MainActivity Code:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById
(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById
(R.id.artist)).getText().toString();
String duration = ((TextView) view.findViewById
(R.id.duration)).getText().toString();
String image=((ImageView)view.findViewById
(R.id.list_image)).getImageMatrix().toString();
//byte[] array=null;
//Bitmap bMap = BitmapFactory.decodeByteArray
// (array, 0, array.length);
// Starting new intent
Intent in = new Intent
(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_ARTIST, artist);
in.putExtra(KEY_DURATION, duration);
in.putExtra(KEY_THUMB_URL, image);
startActivity(in);
}
});
}
}
SingleMenuItemActivity Code:-
public class SingleMenuItemActivity extends Activity {
// XML node keys
private static final String KEY_TITLE = "title";
private static final String KEY_ARTIST = "artist";
private static final String KEY_DURATION = "duration";
private static final String KEY_THUMB_URL = "thumb_url";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String title = in.getStringExtra(KEY_TITLE);
String artist = in.getStringExtra(KEY_ARTIST);
String duration = in.getStringExtra(KEY_DURATION);
String image = in.getStringExtra(KEY_THUMB_URL);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
ImageView lblImage = (ImageView) findViewById(R.id.image_label);
lblName.setText(title);
lblCost.setText(artist);
lblDesc.setText(duration);
//Getting error in below line
//the method setImageResource(int) is the type
//Image View is not applicable for the argument(string)
lblImage.setImageResource(image);//(R.drawable.rihanna);
}
}
Convert this image to bitmap and pass it as bundle using Intent.putExtra() to the next activity.
You don't have to pass the image through Bundle, instead pass the image path as String data, and in each activity, wherever you require image, create image from that image path.
in.putExtra(KEY_IMAGE, image);
where image is byte[] ..Any image can be converted to a bytearray..
and while retieving
Bitmap bMap = BitmapFactory.decodeByteArray(array, 0, array.length);
`

dynamic Bitmap drawable in android

I am wondering how i can set a Bitmap drawable resource from a dynamic variable that is the image name/ type string:
public CustomIcon getView(int position, View convertView, ViewGroup parent) {
String label;
String image;
DataBaseManager db = new DataBaseManager(context);
label = db.getIconLabel(mIcons.get(position));
image = db.getIconImage(mIcons.get(position));
Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(),
parent.getResources().getIdentifier(image, "drawable", getApplicationContext().getPackageName()));
Log.v(TAG, "current pos:"+ position);
CustomIcon icon = new CustomIcon(context, bitmap,label);
return icon;
}
the part in question is
Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(),
R.drawable.[image]);
CODE CHANGED ABOVE -
The way to deal with this is to get the resource ID first and then use the BitmapFactory method:
String imgName = "myicon";
int resID = parent.getResources().getIdentifier(imgName, "drawable", "my.app.package.name");
Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(), resID);

HashMap<String,String> temp = new HashMap<String,String>(); for pass image

for(int i=0;i<StoreparsrData.title.size();i++){
HashMap<String,String> temp = new HashMap<String,String>();
Bitmap bt=Utility.getBitmapFile(StoreparsrData.url.get(i).toString());
ImageView img=(ImageView)findviewbyid(R.id.img);
img.setImageBitmap(bt);
but iwant to convert this img.setImageBitmap(bt); to string so its input for
temp.put("image",image );
temp.put("title",StoreparsrData.title.get(i).toString());
temp.put("description", StoreparsrData.description.get(i).toString());
temp.put("lastbuilddate", StoreparsrData.lastBuildDate.get(0).toString());
list.add(temp);
}
so its show image
Pls Reply me
If I'm understanding your question, why not use:
temp.put("image",StoreparsrData.url.get(i).toString());
Then perform the ImageView lookup when you read from the list
EDIT
Here's another approach:
public class ParseBean {
private ImageView image;
private String title;
private String description;
private String lastBuildDate;
// add getters and setters here
}
...
for(int i=0;i<StoreparsrData.title.size();i++){
HashMap<String,ParseBean> temp = new HashMap<String,ParseBean>();
Bitmap bt=Utility.getBitmapFile(StoreparsrData.url.get(i).toString());
ImageView img=(ImageView)findviewbyid(R.id.img);
img.setImageBitmap(bt);
ParseBean bean = new ParseBean();
bean.setImage(img);
bean.setTitle(StoreparsrData.title.get(i).toString());
bean.setDescription(StoreparsrData.description.get(i).toString());
bean.setLastBuildDate(StoreparsrData.lastBuildDate.get(0).toString());
list.add(bean);
}

Categories

Resources