Moyens de transférer des bitmaps entre les activités

Aactivity.class
     <span style="white-space:pre">		</span>    Drawable mDrawable = imageView.getDrawable();
                    Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("bitmap", bitmap);
                    Intent intent = new Intent(this, PhotoActivity.class);
                    intent.putExtra("bundle", bundle);
                    startActivity(intent);

PhotoActivity.class

        intent = getIntent();
        Bundle bundle=intent.getBundleExtra("bundle");
        Bitmap bitmap=bundle.getParcelable("bitmap");
        imageview.setImageBitmap(bitmap);

Mais la méthode ci-dessus ne transmet pas grand chose, il existe un autre moyen:

 
  
  
Aactivity.class

<span style="white-space:pre">		</span>Intent intent = new Intent(getActivity(), Aactivity.class);
                Drawable mDrawable=imageview.getDrawable();
                Bitmap bitmap=((BitmapDrawable) mDrawable).getBitmap();
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
                intent.putExtra("byteArray", bs.toByteArray());
                startActivity(intent);

PhotoActivity.class

if(getIntent().hasExtra("byteArray")) {
            Bitmap b = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
            imageview.setImageBitmap(b);
        }

La méthode ci-dessus est encore limitée.
Si vous utilisez ImageLoader, vous devez utiliser sa fonction de mise en cache.

  
   
   
ImageLoader.getInstance().displayImage(imageUri,imageView);
String key=DiskCacheUtils.findInCache(imageUri,ImageLoader.getInstance().getDiskCache()).getPath();

otherActivityImageView.setImageURI(Uri.parse(key));
Si le bitmap doit être transmis, en fait, le bitmap doit être mis en cache.



Je suppose que tu aimes

Origine blog.csdn.net/Ser_Bad/article/details/51007887
conseillé
Classement