Assign the Intended bitmap value to an imageView

2019-08-26 08:19发布

I'm using an adapter to load the items to a grid. then when the user select an item from the grid then it opens up the customizing screen. In that process I'm sending some data in the intent and later I can load the these in the customizing screen. Successfully I have loaded the other items other than the isVeg item. Response I'mgetting for isVeg , [false, true, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true] .

My problem is the way I have intented is correct or not. If it is correct how can I assign it to a ImageView.

adapter im using to send the data to next acitivty

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(
                R.layout.pasta_single_item, parent, false);
        holder.ivImage = (ImageView) convertView
                .findViewById(R.id.grid_image);
        holder.tvImageIcon = (ImageView) convertView
                .findViewById(R.id.icon);
        holder.tvHeader = (TextView) convertView
                .findViewById(R.id.grid_text);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tvHeader.setText(descriptions.get(position));
    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);
    }

    holder.tvImageIcon.setImageBitmap(mBitmap);

    Button customizePasta = (Button) convertView
            .findViewById(R.id.bt_direct_customize);
    customizePasta.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent next = new Intent(context, ActivityPastaCustomize.class);
            next.putExtra("description", descriptions.get(position));
            next.putExtra("imageUrl", imageUrls.get(position));
            next.putExtra("price", price.get(position));
            next.putExtra("isVeg", mBitmap);  //intent the image for selected item
            context.startActivity(next);
            ((Activity) context).overridePendingTransition(
                    R.anim.slide_in_right, R.anim.slide_out_left);
        }
    });

    return convertView;
}

private class ViewHolder {
    private TextView tvHeader;
    private ImageView ivImage;
    private ImageView tvImageIcon;
}

}

receiving the data in activity

        final String description = getIntent().getStringExtra("description");
        String imageUrl = getIntent().getStringExtra("imageUrl");
        final String Strprice = getIntent().getStringExtra("price");
        String mBitmap = getIntent().getStringExtra("isVeg"); // recives the item 

setting the recivied data

        final TextView descriptionTV = (TextView) findViewById(R.id.grid_text);
        descriptionTV.setText(description);

        final TextView priceTV = (TextView) findViewById(R.id.pasta_price);
        priceTV.setText("PRICE RS " + Strprice);

        ImageView imageView = (ImageView) findViewById(R.id.grid_image);
        Picasso.with(this).load(imageUrl).into(imageView);

3条回答
▲ chillily
2楼-- · 2019-08-26 08:49

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 -

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/app_name");    
myDir.mkdirs();
String fname = "image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
查看更多
叼着烟拽天下
3楼-- · 2019-08-26 09:02

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

private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
private ImageView tvImageIcon;
}

public String 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:-

String mBitmapString = 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();
  return null;
}
查看更多
孤傲高冷的网名
4楼-- · 2019-08-26 09:05

Instead of sending Bitmap with intent send drawable id.make following changes in getView method:

1. Get selected String from isVeg List:

    @Override
    public void onClick(View view) { 
        ...
        next.putExtra("isVeg", isVeg.get(position));  
        context.startActivity(next);
        ....
    }

2. Receive data in activity isVeg as String:

String strIsVag = 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);
查看更多
登录 后发表回答