TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

Android Intent 如何Base64編解碼傳圖在Imageview秀出

2016-12-15 20:41| 發佈者: woff| 查看: 4219| 評論: 0|原作者: woff

摘要: 在用intent傳資料時以字串居多 但是圖片無法直接傳遞 所以必須先將圖片先給予編碼 到新的Activity時 再給予解碼在imageview秀出 a.java裡 先抓取imageview上的Icon //Get Imageview Drawable and base64 to Intent ...
 
在用intent傳資料時以字串居多
但是圖片無法直接傳遞
所以必須先將圖片先給予編碼
到新的Activity時
再給予解碼在imageview秀出

a.java裡
先抓取imageview上的Icon
  1. //Get Imageview Drawable and base64 to Intent
  2. Bitmap bitmap = ((BitmapDrawable)imageview.getDrawable()).getBitmap();
  3. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  4. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream );
  5. byte bytes[] = stream.toByteArray();
  6. String base64 = Base64.encodeToString(bytes, Base64.DEFAULT);
  7. String icontmp = base64;
  8. Intent i=new Intent();
  9. i.setClass(a.this, b.class);
  10. Bundle b1 =new Bundle();
  11. b1.putString("icon", icontmp);
  12. i.putExtras(b1);
  13. startActivity(i);
複製代碼


b.java裡
最後在imageview秀Icon
  1. //Get Intent String data
  2. label = getpackageinfo.getString("name");
  3. name = getpackageinfo.getString("label");
  4. icon = getpackageinfo.getString("icon");
  5. //decode base64 and Convert bitmap to drawable
  6. byte[] decode = Base64.decode(icon,Base64.DEFAULT);
  7. Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
  8. //Set Icon to Imageview
  9. IV.setImageBitmap(bitmap);
複製代碼

這樣就可以了

10.jpg


參考文章
http://stackoverflow.com/questio ... tached-to-imageview
http://www.cnblogs.com/zhoujian315/p/3272571.html
https://www.kancloud.cn/digest/androidtome/117273
http://blog.csdn.net/lincyang/article/details/46596899

最新評論



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-25 12:15 , Processed in 0.038679 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部