android Bitmap与string互转


  1. 转自:http://blog.csdn.net/csh159/article/details/8835477
  2. /**  
  3.      * 图片转成string  
  4.      *   
  5.      * @param bitmap  
  6.      * @return  
  7.      */  
  8.     public static String convertIconToString(Bitmap bitmap)  
  9.     {  
  10.         ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream  
  11.         bitmap.compress(CompressFormat.PNG, 100, baos);  
  12.         byte[] appicon = baos.toByteArray();// 转为byte数组  
  13.         return Base64.encodeToString(appicon, Base64.DEFAULT);  
  14.   
  15.     }  
  16.   
  17.     /**  
  18.      * string转成bitmap  
  19.      *   
  20.      * @param st  
  21.      */  
  22.     public static Bitmap convertStringToIcon(String st)  
  23.     {  
  24.         // OutputStream out;  
  25.         Bitmap bitmap = null;  
  26.         try  
  27.         {  
  28.             // out = new FileOutputStream("/sdcard/aa.jpg");  
  29.             byte[] bitmapArray;  
  30.             bitmapArray = Base64.decode(st, Base64.DEFAULT);  
  31.             bitmap =  
  32.                     BitmapFactory.decodeByteArray(bitmapArray, 0,  
  33.                             bitmapArray.length);  
  34.             // bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  
  35.             return bitmap;  
  36.         }  
  37.         catch (Exception e)  
  38.         {  
  39.             return null;  
  40.         }  
  41.     }  
  • 1
    点赞
  • 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、付费专栏及课程。

余额充值