android Bitmap与string互转

android Bitmap与string互转

分类: android开发 835人阅读 评论(1) 收藏 举报
  1. /**  
  2.      * 图片转成string  
  3.      *   
  4.      * @param bitmap  
  5.      * @return  
  6.      */  
  7.     public static String convertIconToString(Bitmap bitmap)  
  8.     {  
  9.         ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream  
  10.         bitmap.compress(CompressFormat.PNG, 100, baos);  
  11.         byte[] appicon = baos.toByteArray();// 转为byte数组  
  12.         return Base64.encodeToString(appicon, Base64.DEFAULT);  
  13.   
  14.     }  
  15.   
  16.     /**  
  17.      * string转成bitmap  
  18.      *   
  19.      * @param st  
  20.      */  
  21.     public static Bitmap convertStringToIcon(String st)  
  22.     {  
  23.         // OutputStream out;  
  24.         Bitmap bitmap = null;  
  25.         try  
  26.         {  
  27.             // out = new FileOutputStream("/sdcard/aa.jpg");  
  28.             byte[] bitmapArray;  
  29.             bitmapArray = Base64.decode(st, Base64.DEFAULT);  
  30.             bitmap =  
  31.                     BitmapFactory.decodeByteArray(bitmapArray, 0,  
  32.                             bitmapArray.length);  
  33.             // bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  
  34.             return bitmap;  
  35.         }  
  36.         catch (Exception e)  
  37.         {  
  38.             return null;  
  39.         }  
  40.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Android Bitmap 换为 Icon,您可以使用以下代码: ``` public static Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation Matrix matrix = new Matrix(); // resize the bitmap matrix.postScale(scaleWidth, scaleHeight); // recreate the new bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); return resizedBitmap; } public static void saveImageToInternalStorage(Context context, Bitmap bitmap, String fileName) { FileOutputStream fos = null; try { fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void setIcon(Context context, Bitmap bitmap) { Bitmap resizedBitmap = getResizedBitmap(bitmap, 192, 192); String fileName = "app_icon.png"; saveImageToInternalStorage(context, resizedBitmap, fileName); File file = new File(context.getFilesDir(), fileName); if (file.exists()) { String packageName = context.getPackageName(); PackageManager pm = context.getPackageManager(); ComponentName componentName = new ComponentName(packageName, packageName + ".MainActivity"); ActivityInfo activityInfo = null; try { activityInfo = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } ApplicationInfo applicationInfo = activityInfo.applicationInfo; applicationInfo.icon = new BitmapDrawable(context.getResources(), resizedBitmap); } } ``` 这个代码会将 Bitmap 换为 Icon,并将其保存到应用程序的内部存储中。请注意,这个代码假定您的应用程序的主要活动的名称是 MainActivity,并且您需要修改它以匹配您的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值