Assign The Intended Bitmap Value To An Imageview
Solution 1:
Instead of sending Bitmap with intent send drawable id.make following changes in getView
method:
1. Get selected String from isVeg
List:
@OverridepublicvoidonClick(View view) {
...
next.putExtra("isVeg", isVeg.get(position));
context.startActivity(next);
....
}
2. Receive data in activity isVeg
as String:
StringstrIsVag= getIntent().getStringExtra("isVeg");
3. Set Image to ImageView according to strIsVag :
Bitmap mBitmap;
if (strIsVag.contains("true")) {
mBitmap = BitmapFactory.decodeResource(
this.getResources(), R.drawable.veg);
} else {
mBitmap = BitmapFactory.decodeResource(
this.getResources(), R.drawable.nonveg);
}
ImageView imageView = (ImageView) findViewById(R.id.grid_image);
imageView.setImageBitmap(mBitmap);
Solution 2:
I would suggest to convert Bitmap object to string and send to your desired activity like this:-
next.putExtra("isVeg", BitMapToString(mBitmap));
These this function write below ViewHolder class like this
privateclassViewHolder{
private TextView tvHeader;
private ImageView ivImage;
private ImageView tvImageIcon;
}
publicString BitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
then in the receiving activity convert the string back to bitmap to use it to the imageview like this:-
StringmBitmapString= getIntent().getStringExtra("isVeg");
Bitmap mBitmap=StringToBitMap(mBitmapString);
Assign where you want
image.setImageBitmap(mBitmap);
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
returnnull;
}
Solution 3:
After you download a image from url below is your code-
Picasso.with(this.context).load(imageUrls.get(position))
.into(holder.ivImage);
final String strIsVag=isVeg.get(position);
final Bitmap mBitmap;
if (strIsVag.contains("true")) {
mBitmap = BitmapFactory.decodeResource(
this.context.getResources(), R.drawable.veg);
} else {
mBitmap = BitmapFactory.decodeResource(
this.context.getResources(), R.drawable.nonveg);
}
after that store that bitmap in local storage. and pass path of that storage dir by intent and display it in other activity a you want. below is code for store image in local storage-
FileOutputStream out = new FileOutputStream(file);
mBitmap .compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
hope it will help you.
EDITED
create a file like -
Stringroot= Environment.getExternalStorageDirectory().toString();
FilemyDir=newFile(root + "/app_name");
myDir.mkdirs();
Stringfname="image.jpg";
Filefile=newFile (myDir, fname);
if (file.exists ()) file.delete ();
Post a Comment for "Assign The Intended Bitmap Value To An Imageview"