The implementation of the Bitmap object is passed during Activity jump, activitybitmap

Source: Internet
Author: User

The implementation of the Bitmap object is passed during Activity jump, activitybitmap
Preface

We believe that everyone may understand that we can pass parameters during Activity jump, such as using Intent transmission or Bundle to pass some information about the current Activity to the new Activity to be jumped. But I wonder if you have thought about it. Can we pass a Bitmap object? Some may ask why a Bitmap object needs to be passed? Okay, because I have encountered such a problem during development, let's talk about it.

I want to obtain images from the network and display them on ImageView some time ago. After the images are displayed, I want to click ImageView to jump to a new Activity and display the images I clicked, so how can I get the image on the ImageView I clicked? Next is my focus!

Implementation

At first, I don't know how to do it. Baidu probably understood its ideas after a while:

  • Obtain the image on the ImageView object and generate a Bitmap object.
  • Transcode a Bitmap object to a string
  • Store the string in SharedPreferences.
  • Get the transcoding string from SharedPreferences in the new Activity.
  • Decodes a string into a Bitmap object.
  • Display Bitmap images on ImageView

The idea is quite simple. Next I will introduce how to implement it one by one. The code is dead and can be easily implemented.

1-> Get Bitmap object from ImageView

Bitmap bitmap = ((BitmapDrawable)view.getDrawable()).getBitmap();

2-> transcode Bitmap into strings

        ByteArrayOutputStream baos = new ByteArrayOutputStream();        bitmap.compress(CompressFormat.PNG, 50,baos);        String imageBase64 = new String (Base64.encode(baos.toByteArray(), 0));

3-> store the string in SharedPreferences

        SharedPreferences sPreferences = getSharedPreferences("share", Context.MODE_PRIVATE);        Editor editor = sPreferences.edit();        editor.putString("iamge", imageBase64);        editor.commit();

4-> get the transcoding string from SharedPreferences in the new Activity.

        SharedPreferences sPreferences = getSharedPreferences("share", Context.MODE_PRIVATE);        String imageBase64 = sPreferences.getString("image", "");

5-> decodes a string into a Bitmap object

        byte[] byte64 = Base64.decode(imageBase64, 0);        ByteArrayInputStream bais = new ByteArrayInputStream(byte64);        Bitmap bitmap = BitmapFactory.decodeStream(bais);

6-> display Bitmap images on ImageView

view.setImageBitmap(bitmap);
Summary

The steps are the steps. There is nothing you don't understand. Of course, if you ask me about the principles of transcoding and decoding, I can only say sorry, but I really don't know. Some may ask, after you get the transcoded string, you can simply pass the string directly. Why do you want to save it in SharedPreferences? .. This is a good question. I didn't think of it before, but it's all the same. It's easy to use it by encapsulating this step into a class.

Download source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.