Android之Bitmap转化成Drawable,并将Bitmap转化成.png格式的图片文件

来源:互联网 发布:如何开淘宝品牌店铺 编辑:程序博客网 时间:2024/05/19 05:41

其实这个也没什么太多的东西,我就直接上两个方法,大家自己去研究:

(1)Bitmap->Drawable

  public static Drawable getDrawable(Context ctx, Bitmap bitmap){        
        if (density == 0) {
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(metrics);
            density = metrics.densityDpi;
        }    
        BitmapDrawable d = new BitmapDrawable(bitmap);
        d.setTargetDensity((int) (density * (density * 1.0f / 240)));
        return d;
    }

(2)Bitmap->.png,.jpg格式的文件
    public void saveMyBitmap(String bitName, Bitmap bitmap) throws IOException {
        File f = new File("/sdcard/" + bitName + ".png");
        f.createNewFile();
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



原创粉丝点击