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
- Bitmap bmp= ((bitmapdrawable) order_con_pic.getdrawable ()). Getbitmap ();
- Intent intent=New Intent (orderconfirm. This,showwebimageactivity. class);
- Bytearrayoutputstream baos=New Bytearrayoutputstream ();
- Bmp.compress (Bitmap.CompressFormat.PNG, BAOs);
- byte [] bitmapbyte =baos.tobytearray ();
- Intent.putextra ("bitmap", bitmapbyte);
- 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
- ImageView = (Zoomableimageview) Findviewbyid (R.id.show_webimage_imageview);
- Intent intent=getintent ();
- if (Intent! =null)
- {
- Byte [] Bis=intent.getbytearrayextra ("bitmap");
- Bitmap Bitmap=bitmapfactory.decodebytearray (bis, 0, bis.length);
- Imageview.setimagebitmap (bitmap);
- }
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
- Public void Savemybitmap (String bitname,bitmap mbitmap) throws IOException {
- File F = new file ("/sdcard/note/" + bitname);
- if (!f.exists ())
- F.mkdirs (); //If you do not have this folder, you will be reported file not found error
- f=New File ("/sdcard/note/" +bitname+". png");
- F.createnewfile ();
- try {
- FileOutputStream out = new FileOutputStream (f);
- Mbitmap.compress (Bitmap.CompressFormat.PNG, N, out);
- Out.flush ();
- Out.close ();
- } catch (FileNotFoundException e) {
- LOG.I (Tag,e.tostring ());
- }
- }
2. Pass the Map object:
Encapsulated into bundles:
[Java]View Plaincopy
- Map<string,object> Data=orderlist.get (arg2-1);
- Serializablemap tmpmap=New Serializablemap ();
- Tmpmap.setmap (data);
- Bundle.putserializable ("OrderInfo", Tmpmap);
- Intent.putextras (bundle);
This seralizablemap is a class that implements the Serializable interface in its own package:
[Java]View Plaincopy
- Public class Serializablemap implements Serializable {
- private map<string,object> Map;
- Public map<string,object> Getmap ()
- {
- return map;
- }
- public void Setmap (map<string,object> Map)
- {
- This.map=map;
- }
- }
In order to throw the map object into the bundle,
The method to be removed is:
[Java]View Plaincopy
- Bundle bundle = Getintent (). Getextras ();
- Serializablemap Serializablemap = (SERIALIZABLEMAP) bundle
- . Get ("OrderInfo");
Android Basics-Transfer data between activity (bitmap and Map objects)