Passing android Bitmap Data within activity using Intent in Android

前端 未结 7 1471
梦如初夏
梦如初夏 2020-11-22 09:36

I hava a Bitmap variable named bmp in Activity1 , and I want to send the bitmap to Activity2

Following is the code I use to pass it with the intent.

相关标签:
7条回答
  • 2020-11-22 09:42

    Kotlin Code for send Bitmap to another activity by intent:

    1- in First Activity :

              val i = Intent(this@Act1, Act2::class.java)
               var bStream  =  ByteArrayOutputStream()
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, bStream)
                val byteArray = bStream.toByteArray()
                i.putExtra("image", byteArray )
                startActivity(i)
    

    2- In Activity two (Read the bitmap image) :

     var bitmap : Bitmap? =null
        if (intent.hasExtra("image")){
          //convert to bitmap          
            val byteArray = intent.getByteArrayExtra("image")
            bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
        }
    

    3- if you need to set it as background for a view, like ConstraintLayout or... :

     if (bitmap != null) {
         //Convert bitmap to BitmapDrawable
         var bitmapDrawable = BitmapDrawable( resources , bitmap)
          root_constraintLayout.backgroundDrawable = bitmapDrawable
       }
    
    0 讨论(0)
  • 2020-11-22 09:43

    I would like to also post a best practices answer for those looking to do this (but may be asking the wrong question).

    Instead of passing a bitmap around (which I presume you downloaded from the network, otherwise, you would already have a file reference to it), I recommend using an image loader such as Universal Image Loader to download an image into an ImageView. You can configure it to then cache the image to disk:

     DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                    .defaultDisplayImageOptions(defaultOptions)
                    .build();
    
            ImageLoader.getInstance().init(config);
    

    Now, just pass the image URL in your intent and use the UIL to load the image. In your newly created activity for example, the image will load instantly because it is loading from the cache - even if your image URL has expired since the download.

    0 讨论(0)
  • 2020-11-22 09:46

    We can also solve this without passing data through intent. Just store the image in the memory and in the next Activity just load the image from that location, which can also avoid app crash from passing large bitmap data. Note: You need not even pass the location path to the intent, remember the path and use it.

    0 讨论(0)
  • 2020-11-22 09:48

    Sometimes, the bitmap might be too large for encode&decode or pass as a byte array in the intent. This can cause either OOM or a bad UI experience.

    I suggest to consider putting the bitmap into a static variable of the new activity (the one that uses it) which will carefully be null when you no longer need it (meaning in onDestroy but only if "isChangingConfigurations" returns false).

    0 讨论(0)
  • 2020-11-22 09:51
    Bundle b = new Bundle();
    b.putSerializable("Image", Bitmap);
    mIntent.putExtras(b);
    startActivity(mIntent);
    
    0 讨论(0)
  • 2020-11-22 09:52

    Simply we can pass only Uri of the Bitmap instead of passing Bitmap object. If Bitmap object is Big, that will cause memory issue.

    FirstActivity.

    intent.putExtra("uri", Uri);
    

    From SecondActivity we get back bitmap.

    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(uri));
    
    0 讨论(0)
提交回复
热议问题