Android Basics-Transfer data between activity (bitmap and Map objects)

Source: Internet
Author: User

Original: http://blog.csdn.net/xueerfei008/article/details/23046341

The project needs to use in the 2 activities between the data transfer, before doing are some strings and other things, the results of this card for a long time, tossing an afternoon.

First: Pass bitmap

This problem is very wonderful (probably my Android level is not enough), incredibly do not error, I am directly using bundles or intent extral domain directly stored bitmap, the results of various downtime, various interface disorderly channeling (I am very puzzled) ... After the search to see everyone said can not directly transfer more than 40k pictures, and then in the German Ask forum found the solution. is to store the bitmap as a byte array and then pass through intent.

Of

The code looks like this:

[Java]View Plaincopy
    1. Bitmap bmp= ((bitmapdrawable) order_con_pic.getdrawable ()). Getbitmap ();
    2. Intent intent=New Intent (orderconfirm. This,showwebimageactivity.  class);
    3. Bytearrayoutputstream baos=New Bytearrayoutputstream ();
    4. Bmp.compress (Bitmap.CompressFormat.PNG, BAOs);
    5. byte [] bitmapbyte =baos.tobytearray ();
    6. Intent.putextra ("bitmap", bitmapbyte);
    7. StartActivity (Intent);


The first line of code is how to get its picture from a imageview, this problem is also Daoteng, it seems to use setdrawingcacheenabled also line, because the beginning of this method, but directly between the activity to pass bitmap, This results in a run-time error that was later corrected and no more attempts were attempted.

First new a bytearrayoutputstream stream, and then using the Compress method in bitmap, compress the data into a byte and transfer it.

The method taken out of another activity is:

[Java]View Plaincopy
  1. ImageView = (Zoomableimageview) Findviewbyid (R.id.show_webimage_imageview);
  2. Intent intent=getintent ();
  3. if (Intent! =null)
  4. {
  5. Byte [] Bis=intent.getbytearrayextra ("bitmap");
  6. Bitmap Bitmap=bitmapfactory.decodebytearray (bis, 0, bis.length);
  7. Imageview.setimagebitmap (bitmap);
  8. }

After the byte array is taken out, it is possible to combine the Decodebytearray method in Bitmapfactory with a bitmap.

Plus a stored code:

[Java]View Plaincopy
  1. Public void Savemybitmap (String bitname,bitmap mbitmap) throws IOException {
  2. File F = new file ("/sdcard/note/" + bitname);
  3. if (!f.exists ())
  4. F.mkdirs (); //If you do not have this folder, you will be reported file not found error
  5. f=New File ("/sdcard/note/" +bitname+". png");
  6. F.createnewfile ();
  7. try {
  8. FileOutputStream out = new FileOutputStream (f);
  9. Mbitmap.compress (Bitmap.CompressFormat.PNG, N, out);
  10. Out.flush ();
  11. Out.close ();
  12. } catch (FileNotFoundException e) {
  13. LOG.I (Tag,e.tostring ());
  14. }
  15. }



2. Pass the Map object:

Encapsulated into bundles:

[Java]View Plaincopy
    1. Map<string,object> Data=orderlist.get (arg2-1);
    2. Serializablemap tmpmap=New Serializablemap ();
    3. Tmpmap.setmap (data);
    4. Bundle.putserializable ("OrderInfo", Tmpmap);
    5. Intent.putextras (bundle);


This seralizablemap is a class that implements the Serializable interface in its own package:

[Java]View Plaincopy
  1. Public class Serializablemap implements Serializable {
  2. private map<string,object> Map;
  3. Public map<string,object> Getmap ()
  4. {
  5. return map;
  6. }
  7. public void Setmap (map<string,object> Map)
  8. {
  9. This.map=map;
  10. }
  11. }

In order to throw the map object into the bundle,

The method to be removed is:

[Java]View Plaincopy
      1. Bundle bundle = Getintent (). Getextras ();
      2. Serializablemap Serializablemap = (SERIALIZABLEMAP) bundle
      3. . Get ("OrderInfo");

Android Basics-Transfer data between activity (bitmap and Map objects)

Related Article

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.